40

I have a simple spring-boot project:

-resources
 -application.yaml
 -application-test.yaml

And I have this Dockerfile:

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ADD micro-boot.jar micro-boot.jar
ENTRYPOINT ["java","-Dspring.profiles.active=test" "-jar","/micro-boot.jar"]

1) I build image - C:\micro-boot>docker build -f Dockerfile -t micro-boot .

2) show all images - C:\micro-boot>docker image ls -a

micro-boot   latest  ccc9a75ebc24  4 seconds ago 112MB

3) try to start C:\micro-boot>docker image ls -a

And I get an error:

/bin/sh: [java,-Dspring.profiles.active=test: not found
ip696
  • 6,574
  • 12
  • 65
  • 128
  • Related: https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile – rdas Apr 18 '19 at 18:40
  • 1
    You should passing spring.profiles.active by docker run command or docker-compose which will increase flexibility. Ex: `docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=test"` – huytmb Apr 19 '19 at 01:18

2 Answers2

117

We have 3 ways:

1. Passing Spring Profile in a Dockerfile

FROM openjdk:8-jre-alpine
...
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=test","-jar","app.jar"]

2. Passing Spring Profile in Docker run

docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=test" --name my-app:latest

3. Passing Spring Profile in DockerCompose

version: "3.5"
services:
  my-app:
     image: my-app:latest
     ports:
       - "8080:8080" 
     environment:
       - "SPRING_PROFILES_ACTIVE=test"
huytmb
  • 3,647
  • 3
  • 12
  • 18
  • 3
    imagine you have 4 environments dev sys staging prod, using kubernetes and dynamically you deploy the containers, you can't have different cmd like you ve shown "docker run", in this way you must have a env var of the cluster env to be used inside the docker file like $PROFILE – Tiago Medici Oct 04 '20 at 07:56
  • @TiagoMedici you right, but it broad out of scope of the question. I will update answer for Kubernetes env. Thank you for your attention – huytmb Oct 05 '20 at 03:54
  • "2. Passing Spring Profile in Docker run" rocks! – Igor Kanshyn Aug 20 '21 at 01:55
20

There's a typo here

ENTRYPOINT ["java","-Dspring.profiles.active=test" comma missing here "-jar","/micro-boot.jar"]

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24