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

  • Mount git branches in a container
  • docker container started in Detached mode stopped after process execution
  • Is there a way to specify a suggested tag name inside a Dockerfile?
  • Running docker command from docker container
  • installing cPickle with python 3.5
  • How to copy a folder from docker to other folder?
  • 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
    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

    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.


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


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

    docker run -p 8081:80 username/reponame/finance

    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.

  • Iscsid not starting on a centos6 docker on MacOS host
  • What is the best way to append to /etc/hosts within Dockerfile
  • Docker on Windows: how to connect to container from host using container IP?
  • How to recover from
  • Is CloudFoundry compatible with Docker/CoreOS?
  • docker exe into container does not show container id?
  • 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.