简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM