0

I have a docker-compose.yml file which is setup for two environments, dev and prod:

docker-compose.yml

Contains shared configuration for both environments

version: '3.1'
services:
    homeassistant:
        restart: unless-stopped
        privileged: true
        network_mode: host

docker-compose.dev.yml

Contains configuration for development environment

version: '3.1'
services:
    homeassistant:
        image: registry.gitlab.com/addisonlynch/homeassistant/hass:dev
        container_name: hass-dev

docker-compose.prod.yml

Contains configuration for production environment

version: '3.1'
services:
    homeassistant:
        image: registry.gitlab.com/addisonlynch/homeassistant/hass:prod
        container_name: hass-prod

I can run the dev environment as follows:

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

and the prod environment:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

Both of the environments run flawlessly, however I can only have one running at the same time. If I try to spin up prod while dev is running (on the same host), the dev containers will exit and prod containers will start (effectively replacing them). How do I run both environments at the same time?

Addison Lynch
  • 685
  • 5
  • 12
  • Are these the exact docker compose files? Maybe you have some similar ports ors something else is overlapping – Vahid Mar 21 '20 at 23:56

2 Answers2

3

If you do docker-compose --help you'll see:

 -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)

What this means is that, by default, when you do docker-compose up, Compose manages your services as part of a project which is named after directory name in which docker-compose.yml file is. Since your dev & prod compose files are in the same directory and have the same service names, there's no way for Compose to distinguish one from the other.

With that in mind, in order to have both dev & prod environment running at the same time, you need to specify project name via -p or --project-name flag like:

docker-compose -f docker-compose.yml -f docker-compose.dev.yml -p dev_env up

Another option is to use COMPOSE_PROJECT_NAME environment variable like:

COMPOSE_PROJECT_NAME=dev_env docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

Potential problem in your case could be host network mode, if both dev & prod use the same ports. In that case, the one you start second won't be able to start.

Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21
1

As it is mentioned here you can use -p option to set the project name of the containers and run it multiple times with different prefix names for containers.

docker-compose -p dev

docker-compose -p prod

Vahid
  • 1,265
  • 10
  • 20