Spring Boot in Docker

I am learning how to use Docker with a Spring Boot app. I have run into a small snag and I hope someone can see the issue. My application relies heavily on @Value that are set in environment specific properties files. In my /src/main/resources I have three properties files

  • application.properties
  • application-local.properties
  • application-prod.properties

I normally start my app with:
java -jar -Dspring.profiles.active=local build/libs/finance-0.0.1-SNAPSHOT.jar

  • Installed Go binary not found in path on Alpine Linux Docker
  • docker-compose up postgresql error, chown: changing ownership of ‘/var/lib/postgresql/data’: Operation not permitted
  • DockerFile : Difference between ADD and COPY
  • Unable to access Container url in Bluemix for Spring Boot Application
  • npm commands fails in jenkins pipelines with docker
  • How to move Postresql to RAM disk in Docker?
  • and that reads the “application-local.properties” and runs properly. However, I am using this src/main/docker/DockerFile:

    FROM frolvlad/alpine-oraclejdk8:slim
    VOLUME /tmp
    ADD finance-0.0.1-SNAPSHOT.jar finance.jar
    RUN sh -c 'touch /finance.jar'
    EXPOSE 8081
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /finance.jar" ]
    

    And then I start it as:

    docker run -p 8081:80 username/reponame/finance
    -Dspring.profiles.active=local

    I get errors that my @Values are not found:
    Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘spring.datasource.driverClassName’ in value “${spring.datasource.driverClassName}”

    However, that value does exist in both *.local & *.prop properties files.

    spring.datasource.driverClassName=org.postgresql.Driver
    

    Do I need to do anything special for that to be picked up?

    UPDATE:

    Based upon feedback from M. Deinum I changing my startup to be:

    docker run -p 8081:80 username/reponame/finance
    -eSPRING_PROFILES_ACTIVE=local

    but that didn’t work UNTIL I realized order matter, so now running:

    docker run -e”SPRING_PROFILES_ACTIVE=test” -p 8081:80 username/reponame/finance

    works just fine.

  • Can we deploy an asp.net mvc 4 app to docker with windows container?
  • Jenkins is not waiting for Docker command to finish
  • What is the proper way to “combine” Docker images?
  • Dockerfile and dev/test/prod environment
  • docker run vs. docker-compose up: same image, different outcome
  • Disable CSS and Javascript cache in nginx server created with Docker
  • One Solution collect form web for “Spring Boot in Docker”

    You can use docker run Using Spring Profiles. Running your freshly minted Docker image with Spring profiles is as easy as passing an environment variable to the Docker run command

    $ docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t springio/gs-spring-boot-docker
    

    You can also debug the application in a Docker container. To debug the application JPDA Transport can can be used. So we’ll treat the container like a remote server. To enable this feature pass a java agent settings in JAVA_OPTS variable and map agent’s port to localhost during a container run.

    $ docker run -e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 8080:8080 -p 5005:5005 -t springio/gs-spring-boot-docker
    

    Resource Link:
    Spring Boot with Docker

    Using spring profile with docker for nightly and dev build:

    Simply set the environment varialbe SPRING_PROFILES_ACTIVE when starting the container. This will switch the active of the Spring Application.

    The following two lines will start the latest Planets dev build on port 8081 and the nightly build on port 8080.

    docker run -d -p 8080:8080 -e \"SPRING_PROFILES_ACTIVE=nightly\" --name nightly-planets-server planets/server:nightly
    docker run -d -p 8081:8080 -e \"SPRING_PROFILES_ACTIVE=dev\" --name dev-planets-server planets/server:latest
    

    This can be done automatically from a CI system. The dev server contains the latest build and nightly will be deployed once a day…

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.