skip to Main Content

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


  1. 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 by

    docker compose build
    

    before docker compose up or simply

    docker compose up --build
    
    Login or Signup to reply.
  2. It seems like a docker cache issue, in your docker file:

    COPY ./target/demo-0.0.1-SNAPSHOT.jar app.jar
    

    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:

    docker-compose up –no-cache
    
    Login or Signup to reply.
  3. 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 the docker 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 remove
    the 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 credentials
    and that the Docker container is linking to the correct port.

    To troubleshoot further, you might try running docker ps to verify that your
    containers are running correctly, and then running docker logs to see any error
    messages that might have been logged. Additionally, you might try using

    docker-compose up --build
    

    to rebuild your images and start your containers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search