Rafutk
Rafutk

Reputation: 306

How to deploy multiple databases with one docker-compose.yml using env variables?

This is my docker-compose.yml:

version: "3"
services:
  db:
    container_name: "container_${DB_NAME}"
    image: mysql:5
    restart: always
    environment:
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
    ports:
      - "${DB_PORT}:3306"

And this is my .env.development (one of my .env files):

PORT=3000
DB_HOST=localhost
DB_PORT=3306
DB_NAME=db_dev
DB_USERNAME=root
DB_PASSWORD=root

I use to deploy db service with docker-compose --env-file .env.development up and it works perfectly.

However, if I want to deploy another db service at the same time with another .env file (with for example docker-compose --env-file .env.production up), the first one is stopped and this is the message I get:

Recreating container_db_dev ... done
Attaching to container_db_prod

So my question is, can I use one docker-compose.yml like this one and deploy multiple database services depending on .env given file?

Upvotes: 0

Views: 1760

Answers (1)

You can use the -p command to specific your compose stack name.

docker-compose -p "new_stack" --env-file .env.production up

The answer I get from stack name with docker-compose

Upvotes: 1

Related Questions