I’m still learning how to use Docker. I was trying to add Docker to my Spring Boot app, but it’s not going as smoothly as I expected.
In the beginning, I had an endpoint that returned ‘Hello, World.’ I ran the app with Docker, and it worked. Then, I removed this endpoint and added another one. After that, I added a PostgreSQL database to the app and ran it with Docker again. However, it still only returned the result of the first endpoint that I removed. The second endpoint didn’t exist. I don’t know where I went wrong.
this is my dockerfile
FROM openjdk:17-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ./target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
and this is my docker-compose.yml
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: pfa
3
Answers
You do not say how you’re starting the container(s). I guess you’re just using
docker compose up
. Then you need to update the image: either remove the "old" image or rebuild bybefore
docker compose up
or simplyIt seems like a docker cache issue, in your docker file:
Since this line has not changed its name docker is assuming there aren’t changes in your docker image, so the docker build step is not being considered.
To avoid this issue you must run containers with the following switch:
The Docker image might not be rebuilding: If you made changes to your Spring Boot app after running the
docker build
, the changes might not have been incorporated into the Docker image. Make sure to run thedocker build
again after making changes to your app.The Docker image might not be running the latest version of your app: If you’re
running the Docker image with
docker run
, make sure to use the –rm flag to removethe container after it exits. Otherwise, you might be running an old version of
your app.
The Docker container might not be linking to the correct database: If you added a
PostgreSQL database to your app and the Docker container is not linking to it
correctly, you might see unexpected behavior. Double-check that your
application.properties
file (or equivalent) has the correct database credentialsand that the Docker container is linking to the correct port.
To troubleshoot further, you might try running
docker ps
to verify that yourcontainers are running correctly, and then running
docker logs
to see any errormessages that might have been logged. Additionally, you might try using
to rebuild your images and start your containers.