Skip to content

Commit da11436

Browse files
committed
feat: simplify Dockerfiles by removing build stages and copying pre-built JAR files directly
1 parent f1dc201 commit da11436

File tree

6 files changed

+58
-125
lines changed

6 files changed

+58
-125
lines changed

.github/workflows/deploy.yml

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,21 @@ jobs:
8484
8585
- name: Prepare Docker build context
8686
run: |
87-
mkdir -p vm-deploy/eureka-server vm-deploy/api-gateway vm-deploy/recommendation-service vm-deploy/statistics-service vm-deploy/user-tracking-service
88-
cp $(find eureka-server/target -name "eureka-server*.jar" | grep -v original) vm-deploy/eureka-server/eureka-server.jar
89-
cp $(find api-gateway/target -name "api-gateway*.jar" | grep -v original) vm-deploy/api-gateway/api-gateway.jar
90-
cp $(find recommendation-service/target -name "recommendation-service*.jar" | grep -v original) vm-deploy/recommendation-service/recommendation-service.jar
91-
cp $(find statistics-service/target -name "statistics-service*.jar" | grep -v original) vm-deploy/statistics-service/statistics-service.jar
92-
cp $(find user-tracking-service/target -name "user-tracking-service*.jar" | grep -v original) vm-deploy/user-tracking-service/user-tracking-service.jar
87+
# Create target directories under vm-deploy for each service
88+
mkdir -p vm-deploy/eureka-server/target \
89+
vm-deploy/api-gateway/target \
90+
vm-deploy/recommendation-service/target \
91+
vm-deploy/statistics-service/target \
92+
vm-deploy/user-tracking-service/target
9393
94+
# Copy the pre-built JAR files into the respective target directories with expected names
95+
cp $(find eureka-server/target -type f -name "eureka-server*.jar" | grep -v original) vm-deploy/eureka-server/target/eureka-server-1.0-SNAPSHOT.jar
96+
cp $(find api-gateway/target -type f -name "api-gateway*.jar" | grep -v original) vm-deploy/api-gateway/target/api-gateway-1.0-SNAPSHOT.jar
97+
cp $(find recommendation-service/target -type f -name "recommendation-service*.jar" | grep -v original) vm-deploy/recommendation-service/target/recommendation-service-1.0-SNAPSHOT.jar
98+
cp $(find statistics-service/target -type f -name "statistics-service*.jar" | grep -v original) vm-deploy/statistics-service/target/statistics-service-1.0-SNAPSHOT.jar
99+
cp $(find user-tracking-service/target -type f -name "user-tracking-service*.jar" | grep -v original) vm-deploy/user-tracking-service/target/user-tracking-service-1.0-SNAPSHOT.jar
100+
101+
# Copy the updated Dockerfiles into the respective service directories
94102
cp eureka-server/Dockerfile vm-deploy/eureka-server/
95103
cp api-gateway/Dockerfile vm-deploy/api-gateway/
96104
cp recommendation-service/Dockerfile vm-deploy/recommendation-service/
@@ -147,46 +155,15 @@ jobs:
147155
148156
- name: Deploy to Oracle VM
149157
run: |
150-
ssh -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa opc@${{ secrets.ORACLE_VM_IP }} "mkdir -p ~/music-analytics/vm-deploy/{eureka-server,api-gateway,recommendation-service,statistics-service,user-tracking-service}"
151-
152-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/eureka-server/eureka-server.jar vm-deploy/eureka-server/Dockerfile opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/eureka-server/
153-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/api-gateway/api-gateway.jar vm-deploy/api-gateway/Dockerfile opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/api-gateway/
154-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/recommendation-service/recommendation-service.jar vm-deploy/recommendation-service/Dockerfile opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/recommendation-service/
155-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/statistics-service/statistics-service.jar vm-deploy/statistics-service/Dockerfile opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/statistics-service/
156-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/user-tracking-service/user-tracking-service.jar vm-deploy/user-tracking-service/Dockerfile opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/user-tracking-service/
158+
# Create the base directory on the remote VM
159+
ssh -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa opc@${{ secrets.ORACLE_VM_IP }} "mkdir -p ~/music-analytics/vm-deploy"
157160
158-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa cloud-deploy/docker-compose.direct.yml opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/docker-compose.yml
159-
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa vm-deploy/.env opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/.env
161+
# Recursively copy each service directory to the remote VM
162+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa -r vm-deploy/eureka-server opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/eureka-server
163+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa -r vm-deploy/api-gateway opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/api-gateway
164+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa -r vm-deploy/recommendation-service opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/recommendation-service
165+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa -r vm-deploy/statistics-service opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/statistics-service
166+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa -r vm-deploy/user-tracking-service opc@${{ secrets.ORACLE_VM_IP }}:~/music-analytics/vm-deploy/user-tracking-service
160167
161-
ssh -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa opc@${{ secrets.ORACLE_VM_IP }} "cd ~/music-analytics/vm-deploy && \
162-
echo 'Logging in to Docker registry...' && \
163-
echo \"${{ secrets.OCI_AUTH_TOKEN }}\" | docker login ${{ secrets.OCI_REGISTRY }} -u ${{ secrets.OCI_USERNAME }} --password-stdin && \
164-
echo 'Stopping and removing existing containers if any...' && \
165-
docker ps -q | xargs -r docker stop && docker ps -aq | xargs -r docker rm && \
166-
echo 'Building Docker images locally...' && \
167-
for service in eureka-server api-gateway recommendation-service statistics-service user-tracking-service; do \
168-
docker build -t music-analytics/\$service ./\$service || exit 1; \
169-
done && \
170-
echo 'Fixing docker-compose file (removing version attribute)...' && \
171-
sed -i '/^version:/d' docker-compose.yml && \
172-
echo 'Starting new containers with docker-compose...' && \
173-
docker-compose up -d && \
174-
echo 'Container status:' && docker-compose ps"
175-
176-
- name: Debug on failure
177-
if: failure()
178-
run: |
179-
if [ -f "$HOME/.ssh/id_rsa" ]; then
180-
ssh -o StrictHostKeyChecking=no -i $HOME/.ssh/id_rsa opc@${{ secrets.ORACLE_VM_IP }} '
181-
echo "==== ENVIRONMENT VARIABLES ====" &&
182-
env | grep DOCKER || echo "No Docker environment variables found" &&
183-
echo "==== DOCKER-COMPOSE FILE CONTENT ====" &&
184-
cat ~/music-analytics/vm-deploy/docker-compose.yml || echo "No docker-compose file found" &&
185-
echo "==== EUREKA SERVER LOGS ====" &&
186-
docker logs $(docker ps -q -f name=eureka-server) || echo "No eureka-server logs available" &&
187-
echo "==== ALL CONTAINERS ====" &&
188-
docker ps -a || echo "No containers found"
189-
'
190-
else
191-
echo "SSH key file not found, cannot retrieve logs"
192-
fi
168+
# Copy the docker-compose file and environment file to the remote VM
169+
scp -o StrictHostKeyChecking=no -i $HOME/.ssh/id

api-gateway/Dockerfile

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# Use an official Maven image to build the application with Java 8
2-
FROM maven:3.8.5-openjdk-8-slim AS build
3-
4-
# Set the working directory inside the container
5-
WORKDIR /app
6-
7-
# Copy the pom.xml file and download the dependencies
8-
COPY pom.xml .
9-
RUN mvn dependency:go-offline
10-
11-
# Copy the source code and build the application
12-
COPY src ./src
13-
RUN mvn package -DskipTests
14-
151
# Use an official OpenJDK image to run the application
162
FROM openjdk:8-jre-slim
173

@@ -21,8 +7,8 @@ WORKDIR /app
217
# Install curl for health checks
228
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
239

24-
# Copy the built JAR file from the build stage
25-
COPY --from=build /app/target/api-gateway-1.0-SNAPSHOT.jar app.jar
10+
# Copy the pre-built JAR file into the container
11+
COPY target/api-gateway-1.0-SNAPSHOT.jar app.jar
2612

2713
# Expose the port that the application will run on
2814
EXPOSE 8080

eureka-server/Dockerfile

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
# First stage: Build the application
2-
FROM maven:3.8.5-openjdk-8-slim AS builder
3-
WORKDIR /app
4-
COPY pom.xml .
5-
RUN mvn dependency:go-offline
6-
COPY src ./src
7-
RUN mvn package -DskipTests
8-
# Set up Java runtime (if needed)
9-
RUN mkdir -p /javaruntime && \
10-
cp -R $JAVA_HOME/* /javaruntime/
1+
# Use an official OpenJDK image to run the application
2+
FROM openjdk:8-jre-slim
113

12-
# Second stage: Create the runtime image
13-
FROM alpine:3.18
14-
RUN apk add --no-cache openjdk8-jre-base
4+
# Set the working directory inside the container
155
WORKDIR /app
16-
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
17-
ENV PATH="${JAVA_HOME}/bin:${PATH}"
18-
COPY --from=builder /app/target/eureka-server-*.jar /app/app.jar
6+
7+
# Install curl for health checks
8+
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
9+
10+
# Copy the pre-built JAR file into the container
11+
COPY target/eureka-server-1.0-SNAPSHOT.jar app.jar
12+
13+
# Expose the port that the application will run on
1914
EXPOSE 8761
20-
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
15+
16+
# Set the entry point to run the application
17+
ENTRYPOINT ["java", "-jar", "app.jar"]

recommendation-service/Dockerfile

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# Use an official Maven image with Java 8 to match pom.xml configuration
2-
FROM maven:3.8.5-openjdk-8-slim AS build
3-
4-
# Set the working directory inside the container
5-
WORKDIR /app
6-
7-
# Copy the pom.xml file and download the dependencies
8-
COPY pom.xml .
9-
RUN mvn dependency:go-offline
10-
11-
# Copy the source code and build the application
12-
COPY src ./src
13-
RUN mvn package -DskipTests
14-
151
# Use an official OpenJDK image to run the application
162
FROM openjdk:8-jre-slim
173

@@ -21,8 +7,8 @@ WORKDIR /app
217
# Install curl for health checks
228
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
239

24-
# Copy the built JAR file from the build stage (with correct path)
25-
COPY --from=build /app/target/recommendation-service-1.0-SNAPSHOT.jar ./app.jar
10+
# Copy the pre-built JAR file into the container
11+
COPY target/recommendation-service-1.0-SNAPSHOT.jar app.jar
2612

2713
# Expose the port that the application will run on
2814
EXPOSE 8082

statistics-service/Dockerfile

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# Use an official Maven image to build the application
2-
FROM maven:3.8.5-openjdk-8-slim AS build
3-
4-
# Set the working directory inside the container
5-
WORKDIR /app
6-
7-
# Copy the pom.xml file and download the dependencies
8-
COPY pom.xml .
9-
RUN mvn dependency:go-offline
10-
11-
# Copy the source code and build the application
12-
COPY src ./src
13-
RUN mvn package -DskipTests
14-
151
# Use an official OpenJDK image to run the application
162
FROM openjdk:8-jre-slim
173

@@ -21,8 +7,8 @@ WORKDIR /app
217
# Install curl for health checks
228
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
239

24-
# Copy the built JAR file from the build stage
25-
COPY --from=build /app/target/statistics-service-1.0-SNAPSHOT.jar app.jar
10+
# Copy the pre-built JAR file into the container
11+
COPY target/statistics-service-1.0-SNAPSHOT.jar app.jar
2612

2713
# Expose the port that the application will run on
2814
EXPOSE 8083

user-tracking-service/Dockerfile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# Build stage - adjusted to use Java 8 for consistency with parent pom.xml
2-
FROM maven:3.8.5-openjdk-8-slim AS build
3-
WORKDIR /app
4-
COPY pom.xml .
5-
COPY src ./src
6-
RUN mvn clean package -DskipTests
7-
# List files to debug
8-
RUN ls -la target/
9-
10-
# Runtime stage - changed from Java 17 to Java 8
1+
# Use an official OpenJDK image to run the application
112
FROM openjdk:8-jre-slim
3+
4+
# Set the working directory inside the container
125
WORKDIR /app
6+
7+
# Install curl for health checks
138
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
14-
# Use specific JAR name instead of wildcard
15-
COPY --from=build /app/target/user-tracking-service-1.0-SNAPSHOT.jar ./app.jar
16-
CMD ["java", "-jar", "app.jar"]
9+
10+
# Copy the pre-built JAR file into the container
11+
COPY target/user-tracking-service-1.0-SNAPSHOT.jar app.jar
12+
13+
# Expose the port that the application will run on
14+
EXPOSE 8084
15+
16+
# Set the entry point to run the application
17+
ENTRYPOINT ["java", "-jar", "app.jar"]

0 commit comments

Comments
 (0)