Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 550
sonoerin Avatar asked Apr 30 '17 14:04

sonoerin


People also ask

Is Spring Boot good for Docker?

Running your Spring Boot application in a Docker container has numerous benefits. First, Docker's friendly, CLI-based workflow lets developers build, share, and run containerized Spring applications for other developers of all skill levels.

What is Spring Boot Docker?

Docker simplifies and accelerates your workflows by letting you freely innovate with your choice of tools, application stacks, and deployment environments for each project. You can run your Spring Boot artifact directly within Docker containers. This is useful when you need to quickly create microservices.


1 Answers

There are 3 different ways to do this, as explained here

  1. Passing Spring Profile in Dockerfile
  2. Passing Spring Profile in Docker run command
  3. Passing Spring Profile in DockerCompose

Below an example for a spring boot project dockerfile

<pre>FROM java:8
ADD target/my-api.jar rest-api.jar
RUN bash -c 'touch /pegasus.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=dev","-jar","/rest-api.jar"]
</pre>

You can use the docker run command

docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:latest

If you intend to use the docker compose you can use something like this

version: "3"
services:
  rest-api:
     image: rest-api:0.0.1
     ports:
       - "8080:8080" 
     environment:
       - "SPRING_PROFILES_ACTIVE=dev"  

More description and examples can be found here

like image 79
Abhishek Galoda Avatar answered Sep 28 '22 15:09

Abhishek Galoda