0

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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It tries to create another service as expected but mysql port (3306) is already in use... It says Bind for 0.0.0.0:3306 failed: port is already allocated
One port can only bind to 1 container/service. You can change the DB_PORT in your env file right.
Oh I thought it was because of mysql static port. I changed DB_PORT in .env.production and now I have the two containers up. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.