http://www.littlebigextra.com/use-spring-profiles-docker-containers/
How to use Spring Profiles with Docker Containers
Introduction
Spring Profiles are an effective way of implementing environment independent code. The properties file or @Beans can be selected dynamically at run time based on the profile injected.
Assuming that you are quite familiar with the spring profiles and looking for injecting profiles in a Docker environment. There are couple of ways of doing it namely
- Passing Spring Profile in Dockerfile
- Passing Spring Profile in Docker run command
- Passing Spring Profile in DockerCompose
In this tutorial, I will try to capture all these 3 scenarios.
Read Here: How to create Docker image of Standalone Spring MVC project
Passing Spring Profile in a Dockerfile
From command prompt of your system, any spring boot application can be run with “java -jar” command.The profiles need to be passed as an argument like this “-Dspring.profiles.active=dev“. For Spring MVC applications other 2 below methods will work fine.
1 2 3 | java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=dev -jar rest-api.jar |
Similarly, when using dockerfile we need to pass the profile as an argument, have a look at one of the Dockerfile for creating a spring boot docker image
Below an example on spring boot project dockerfile
1 2 3 4 5 6 | 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"] |
Pay attention to the last line ENTRYPOINT, in this line we are passing the java command to execute the jar file and all arguments have been passed as comma separated values. “-Dspring.profiles.active=dev” is where we are passing the dev profile, you can replace dev with the profile name you want.
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.
1 2 3 | docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:latest |
Passing Spring Profile in DockerCompose
If you are on DockerSwarm or using compose file for deploying docker images the Spring profile can be passed using the environment: tag in a docker-compose file, as shown below
1 2 3 4 5 6 7 8 9 10 | version: "3" services: rest-api: image: rest-api:0.0.1 ports: - "8080:8080" environment: - "SPRING_PROFILES_ACTIVE=dev" |
'SERVER > DOCKER 도커' 카테고리의 다른 글
[DOCKER]도커 컴포즈를 활용하여 완벽한 개발 환경 구성하기 (0) | 2019.08.01 |
---|---|
도커 .. 다시 하려니 기억이 하나도 안남. 재정리 (0) | 2019.03.14 |
도커 이미지, ps 전체삭제 on windows (0) | 2019.01.23 |
[도커] spring boot + gradle 배포 정리. (3) | 2019.01.18 |
Docker Network 구조(1) - docker0와 container network 구조 (0) | 2019.01.09 |
댓글