skip to Main Content

I am creating a simple Java/Gradle/Spring application. I am working on Dockerizing the app. When I run the app locally without docker, it properly uses the values in my application.yml file. However, when I run the Docker image, it doesn’t use any of the values from the application.yml file.

In my Dockerfile below, you’ll see that I copy the application.yml file to the same directory as my jar in the docker image. Additionally, in the entrypoint, I reference the application.yml file appropriately (when I change this file path to something that is incorrect, the app breaks on start-up).

Dockerfile:

FROM eclipse-temurin:17-jdk
EXPOSE 8080
RUN mkdir -p /app/
ADD build/libs/image-name-0.0.1-SNAPSHOT.jar /app/image-name.jar
ADD build/resources/main/application.yml /app/application.yml
ENTRYPOINT ["sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /app/image-name.jar --spring.config.location=file:/app/application.yml"]

Below is my docker-compose.yml file. I don’t think this matters much as I got the same result by just using the docker run ... command, but I’d like to add it for context:

version: '2'

services:
  image-name:
    image: 'image-name:latest'
    container_name: image-name
    ports:
      - "8081:8080"
    build:
      context: .
    depends_on:
      - postgres_db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
      - SPRING_DATASOURCE_USERNAME=admin
      - SPRING_DATASOURCE_PASSWORD=password
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update

  postgres_db:
    image: 'postgres:13.1-alpine'
    container_name: postgres_db
    environment:
      - POSTGRES_USER=admin # TODO: CHANGE THIS LATER
      - POSTGRES_PASSWORD=password # TODO: CHANGE THIS LATER

I have added the Spring Actuator dependency and exposed the health endpoint using the application.yml. However, when I run this via the Docker image, the log below is missing (but is present when I run locally without Docker). I also get a 404 when I hit the /actuator/health endpoint:

2022-12-26T15:07:08.439-06:00  INFO 23289 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'

Any idea as to what I am doing incorrectly? I have looked at seemingly every other related Stack Overflow thread with no luck.

2

Answers


  1. Chosen as BEST ANSWER

    Okay, I managed to solve this, and I really hope this helps someone in the future as well because I was banging my head against the wall over this for days. I made the change that Elyorbek Ibrokhimov had mentioned, with no luck. I even re-wrote everything in its most basic form so that it was essentially verbatim from the docs with no luck.

    I then decided to simply change my configuration to change my yaml file to a sub-environment yaml file by renaming it to application-local.yaml and then setting the active profile to local and it worked. I think I will have to dive into the docs on how application.yaml files are processed after learning this. I will update this with any findings I make. Perhaps someone who understands why this happens can add their own answer.

    Below is my final Dockerfile:

    FROM eclipse-temurin:17-jdk
    EXPOSE 8080
    RUN mkdir -p /app/
    ADD build/libs/image-name-0.0.1-SNAPSHOT.jar /app/image-name.jar
    ADD build/resources/main/application-local.yml /app/application-local.yml
    ENTRYPOINT ["sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -Dspring.config.location=file:/app/application-local.yml -Dspring.profiles.active=local -jar /app/image-name.jar"]
    

    I will not mark this as the accepted answer until I fully understand why this works the way it does.


  2. Options should come first.

    java [options] -jar jarfile [args ...]
    

    In your case should be:

    java -Dspring.config.location=/app/application.yml -jar /app/image-name.jar 
    

    Reference documentation

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