Home > OS >  How to choose correct profile on different environments for Docker and Docker-Compose?
How to choose correct profile on different environments for Docker and Docker-Compose?

Time:12-24

Actually I have checked some questions like this

What I do not understand is; if I change my docker-compose.yml and add profile to it then should I leave the Dockerfile without profile ? For example my docker-compose file:

backend:
    container_name: backend
    image: backend
    build: ./backend
    restart: always
    deploy:
      restart_policy:
        condition: on-failure
        max_attempts: 15
    ports:
      - '8080:8080'
    environment:
      - MYSQL_ROOT_PASSWORD=DbPass3008
      - MYSQL_PASSWORD=DbPass3008
      - MYSQL_USER=DbUser
      - MYSQL_DATABASE=db
    depends_on:
      - mysql

And I will add:

environment:
       - "SPRING_PROFILES_ACTIVE=test

As far as I understand I need to put 3 different compose file and run them with -f parameter for different environments like:

docker-compose -f docker-compose-local/test/prod up -d

But my question is that my Dockerfile is already specifying profile as:

FROM openjdk:17-oracle
ADD ./target/backend-0.0.1-SNAPSHOT.jar backend.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=TEST", "backend.jar"]

So how should I change this Dockerfile? Even if I create 3-4 different compose file, they are all using same Dockerfile. Should I create different Dockerfiles too (seems ridicilous) but what is the correct way ?

CodePudding user response:

There's no need to add a java -Dspring.profiles.active=... command-line option; Spring will recognize the runtime SPRING_PROFILES_ACTIVE environment variable on its own. That means all of your environments can use the same image (which is generally a good practice).

Compose can also expand host environment variables in some contexts, so you may be able to use a single Compose file with environment-variable references

version: '3.8'
services:
  backend:
    environment:
      - SPRING_PROFILES_ACTIVE=${ENVIRONMENT:-dev}
ENVIRONMENT=test docker-compose up -d

I tend to discourage putting environment-specific settings in a src/main/resources/*.yml file, since it means you need to recompile the application jar file whenever you deploy to a new environment. Another possibility is to set most Spring properties as environment variables, and then use multiple Compose files to include environment-specific settings. The one downside here is that you need multiple docker-compose -f options and you need to repeat them on every docker-compose invocation.

  • Related