I have a spring boot application run in Docker and it can be run successfully. The problem is that when I update my application code, the code changes is not reflected on Docker after rebuilding the image and start the container.
Here is my Dockerfile. I try to copy the src file into the image and packaging the spring boot application on build stage. Then copy the jar generated to another stage and run the application when the container is started.
FROM openjdk:17 as buildstage
WORKDIR /app
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
RUN ./mvnw dependency:go-offline
COPY src src
RUN ./mvnw package -Dmaven.test.skip=true
COPY target/*.jar springboot-flash-cards-docker.jar
FROM openjdk:17
COPY --from=buildstage /app/springboot-flash-cards-docker.jar .
ENTRYPOINT ["java", "-jar", "springboot-flash-cards-docker.jar"]
And this is parts of my docker-compose.yml for the spring boot application.
services:
container_name: springboot
build:
context: ./springboot-flash-cards
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
- postgres
links:
- postgres
I have checked that the code in the springboot container is not updated after editing my code base. The way I check:
docker cp "springboot container":springboot-flash-cards-docker.jar .
java xf springboot-flash-cards-docker.jar
I tried to use docker compose build --no-cache to prevent docker build image from cache. But doesn't work.
Expected Behaviour: Dode changes in the spring boot application will be reflected after rebuilding the image and run inside the docker container.
Thank you for the help from @DavidMaze. The problem is fixed.
Reason why problem occur:
The docker run command runs a command in a new container, pulling the image if needed and starting the container. From Docker doc
In the original Dockerfile, RUN ./mvnw package -Dmaven.test.skip=true build the jar file inside the container.
COPY target/*.jar springboot-flash-cards-docker.jar will copy the jar file "target/*.jar" from the host machine to the container. And what I expected is to move and rename the jar built inside the docker container. Since I didn't package code on my host after code changes, the code changes is not reflected on docker.
How to fixed it:
Use RUN mv target/*.jar springboot-flash-cards-docker.jar instead of COPY target/*.jar springboot-flash-cards-docker.jar. This command will move and rename the
"target/*.jar" inside the container to "springboot-flash-cards-docker.jar".
The fixed docker file:
FROM openjdk:17 as buildstage
WORKDIR /app
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
RUN ./mvnw dependency:go-offline
COPY src src
RUN ./mvnw package -Dmaven.test.skip=true
RUN mv target/*.jar springboot-flash-cards-docker.jar
FROM openjdk:17
COPY --from=buildstage /app/springboot-flash-cards-docker.jar .
ENTRYPOINT ["java", "-jar", "springboot-flash-cards-docker.jar"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With