Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start spring boot application in docker with profile?

Tags:

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
like image 429
ip696 Avatar asked Apr 18 '19 18:04

ip696


People also ask

How do I pass a spring profile active in docker?

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.

How do I deploy a spring boot application using docker?

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.

How do I run spring boot Microservices in docker?

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.

Which file is used to create a docker container for a spring boot app?

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.


1 Answers

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"
like image 134
huytmb Avatar answered Oct 22 '22 12:10

huytmb