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
Passing Spring Profile in Docker run You can also pass spring profile as an environment variable while using docker run command using the -e flag. The option -e “SPRING_PROFILES_ACTIVE=dev” will inject the dev profile to the Docker container.
Dockerize a Standalone Spring Boot Application This file contains the following information: FROM: As the base for our image, we'll take the Java-enabled Alpine Linux created in the previous section. MAINTAINER: The maintainer of the image. COPY: We let Docker copy our jar file into the image.
Run maven command - clean install, and a jar file gets created in the target folder. Next we will start docker and deploy this jar using docker. Now open the terminal and go to the Spring Boot project folder. Next we will build an image with the name producer.
This Dockerfile is very simple, but it is all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build creates a spring user and a spring group to run the application. It is then copied (by the COPY command) the project JAR file into the container as app.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With