Skip to content

Commit 18b8bd0

Browse files
authored
Merge pull request #17 from CapstoneProjectCMC/coding-service/Fix-CodingDocker
HardCode Fix Coding Service
2 parents 2572150 + 00062d8 commit 18b8bd0

File tree

7 files changed

+113
-6
lines changed

7 files changed

+113
-6
lines changed

build-image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ main() {
3939
login
4040
echo "Building with DOCKER_GID=${DOCKER_GID}"
4141

42-
for svc in coding-service; do
42+
for svc in search-service profile-service identity-service; do
4343
echo "Building $svc..."
4444
build_push_java "$svc"
4545
done

common-protos/src/main/proto/coding.proto

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ message SubmitCodeRequest {
5454
string studentId = 2;
5555
string language = 3;
5656
string sourceCode = 4;
57-
int32 memoryMb = 5;
58-
float cpus = 6;
59-
int32 timeTakenSeconds = 7;
57+
int32 timeTakenSeconds = 5;
6058
}
6159

6260
message TestCaseResultDto {
@@ -74,6 +72,9 @@ message SubmitCodeResponse {
7472
int32 totalPoints = 3;
7573
bool passed = 4;
7674
repeated TestCaseResultDto results = 5;
75+
int32 memoryMb = 6;
76+
float cpus = 7;
77+
int32 peakMemoryKb = 8;
7778
}
7879

7980
message LoadCodingRequest {

common-protos/src/main/proto/submission_sync.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ message CodeSubmissionDto {
3535
google.protobuf.Timestamp submittedAt = 8;
3636
int32 timeTakenSeconds = 9;
3737
repeated TestCaseResultSyncDto results = 10;
38+
int32 peakMemoryKb = 11;
39+
float cpus = 12;
40+
int32 memoryMb = 13;
3841
}
3942

4043
message TestCaseResultSyncDto {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# ===== Build stage =====
2+
FROM maven:3.9.9-eclipse-temurin-21 AS build
3+
4+
ARG MODULE
5+
WORKDIR /workspace
6+
7+
# Copy pom gốc và các module để cache dependency
8+
COPY pom.xml .
9+
COPY common-protos/pom.xml common-protos/pom.xml
10+
COPY common-events/pom.xml common-events/pom.xml
11+
COPY gateway-service/pom.xml gateway-service/pom.xml
12+
COPY identity-service/pom.xml identity-service/pom.xml
13+
COPY profile-service/pom.xml profile-service/pom.xml
14+
COPY submission-service/pom.xml submission-service/pom.xml
15+
COPY quiz-service/pom.xml quiz-service/pom.xml
16+
COPY coding-service/pom.xml coding-service/pom.xml
17+
COPY ai-service/pom.xml ai-service/pom.xml
18+
COPY search-service/pom.xml search-service/pom.xml
19+
COPY notification-service/pom.xml notification-service/pom.xml
20+
COPY chat-service/pom.xml chat-service/pom.xml
21+
22+
23+
# Tải dependency trước để cache (Không compile)
24+
RUN mvn -q -DskipTests dependency:go-offline
25+
RUN apt-get update && \
26+
apt-get install -y \
27+
apt-transport-https \
28+
ca-certificates \
29+
curl \
30+
gnupg && \
31+
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \
32+
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian buster stable" > /etc/apt/sources.list.d/docker.list && \
33+
apt-get update && \
34+
apt-get install -y docker-ce-cli
35+
36+
## Copy toàn bộ source
37+
COPY . .
38+
39+
# Build & install proto + events (tạo jar trước)
40+
RUN mvn -q -DskipTests install -pl common-protos,common-events
41+
42+
# Build đúng module
43+
RUN mvn -q -DskipTests -pl ${MODULE} -am package
44+
45+
# ===== Runtime stage =====
46+
FROM eclipse-temurin:21-jre
47+
48+
# Build-args
49+
ARG MODULE
50+
ARG DOCKER_HOST_GID=999
51+
52+
# Cài đặt Docker CLI trong runtime image
53+
RUN apt-get update && \
54+
apt-get install -y \
55+
apt-transport-https \
56+
ca-certificates \
57+
curl \
58+
gnupg \
59+
sudo && \
60+
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \
61+
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian buster stable" > /etc/apt/sources.list.d/docker.list && \
62+
apt-get update && \
63+
apt-get install -y docker-ce-cli
64+
65+
# Tạo group và user cho ứng dụng (chỉ cho coding-service)
66+
RUN if [ "$MODULE" = "coding-service" ]; then \
67+
groupadd -r -g ${DOCKER_HOST_GID} docker_host && \
68+
groupadd -r -g 1001 appuser && \
69+
useradd -r -u 1001 -g appuser -G docker_host appuser && \
70+
echo "appuser ALL=(root) NOPASSWD: /usr/bin/docker" >> /etc/sudoers; \
71+
fi
72+
73+
# TẠO THƯ MỤC /WORK VÀ CẤP QUYỀN
74+
RUN if [ "$MODULE" = "coding-service" ]; then \
75+
mkdir -p /work && chown -R 1001:1001 /work; \
76+
fi
77+
78+
# Thiết lập thư mục làm việc
79+
WORKDIR /app
80+
81+
# Copy JAR với quyền sở hữu phù hợp
82+
COPY --from=build /workspace/${MODULE}/target/*.jar app.jar
83+
84+
# Đặt quyền sở hữu cho coding-service
85+
RUN if [ "$MODULE" = "coding-service" ]; then \
86+
chown appuser:appuser app.jar; \
87+
fi
88+
89+
# Đảm bảo quyền đọc
90+
RUN chmod +r app.jar
91+
92+
# Chuyển sang sử dụng user appuser (chỉ cho coding-service)
93+
USER ${MODULE:+-}${MODULE:+appuser}
94+
95+
ENV JAVA_OPTS=""
96+
EXPOSE 7777
97+
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar /app/app.jar"]

docker/java-service.Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ RUN mvn -q -DskipTests -pl ${MODULE} -am package
4545
# ===== Runtime stage =====
4646
FROM eclipse-temurin:21-jre
4747

48+
# Thiết lập thư mục làm việc
49+
WORKDIR /app
50+
51+
# Copy JAR với quyền sở hữu phù hợp
52+
COPY --from=build /workspace/${MODULE}/target/*.jar app.jar
53+
54+
# Đặt quyền sở hữu cho coding-service
55+
FROM eclipse-temurin:21-jre
4856
# Build-args
4957
ARG MODULE
5058
ARG DOCKER_HOST_GID=999

search-service/src/main/resources/application-docker.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ spring:
1212
connection-timeout: 10s
1313
lifecycle:
1414
timeout-per-shutdown-phase: 20s
15-
1615
kafka:
1716
bootstrap-servers: http://kafka:9092
1817

search-service/src/main/resources/application.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ spring:
1212
uris: http://localhost:9200
1313
username: elasticsearch_search
1414
password: dinhanst2832004
15-
1615
kafka:
1716
bootstrap-servers: localhost:9094
1817
consumer:

0 commit comments

Comments
 (0)