Docker compose orphan containers when overriding services
Asked Answered
A

1

15

I want to have two docker-compose files, where one overrides another.
(The motivation comes from Docker Compose Docs)
The use case comes from the buildbot environment. The first docker-compose file should define a simple service. This is a service that is going to be tested. Let's take

version: '2'
services:
  service-node:
    build:
      context: ./res
      dockerfile: Dockerfile
    image: my/server
    env_file: .env

The second docker-compose file (let's name it docker-compose.test.yml) overrides the service-node to add a buildbot worker feature, and creates the second container, i.e. buildbot master node, that is going to control testing machinery. Let's take

version: '2'
services:
  service-node:
    build:
      context: ./res
      dockerfile: buildbot.worker.Dockerfile
    image: my/buildbot-worker
    container_name: bb-worker
    env_file: ./res/buildbot.worker.env
    environment:
      - BB_RES_DIR=/var/lib/buildbot
    networks:
      testlab:
        aliases:
          - bb-worker
    volumes:
      - ./vol/bldbot/worker:/home/bldbotworker
    depends_on:
      - bb-master

  bb-master:
    build:
      context: ./res
      dockerfile: buildbot.master.Dockerfile
    image: my/buildbot-master
    container_name: bb-master
    env_file: ./res/buildbot.master.env
    environment:
      - BB_RES_DIR=/var/lib/buildbot
    networks:
      - testlab
    expose:
      - "9989"
    volumes:
      - ./vol/bldbot/master:/var/lib/buildbot

networks:
  testlab:
    driver: bridge

Generally this configuration works, i.e. the command

docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d 

builds both images and runs both containers, but there is one shortcoming, i.e. the command

docker-compose ps

shows only one service, bb-worker. At the same time

docker ps

shows both.
Furthermore, the command

docker-compose down

stops only one service, and outputs the message/warning Found orphan containers. Of course, the message refers to bb-master.

How can I override the basic docker-compose.yml file to be able to add additional non-orphan service?

Aphaeresis answered 5/1, 2017 at 21:0 Comment(0)
D
26

You need to run all docker-compose commands with the flags, e.g.:

docker-compose -f docker-compose.yml -f docker-compose.test.yml down

Alternatively, you can make this the default by writing the following to a .env file in the same folder:

COMPOSE_FILE=docker-compose.yml:docker-compose.test.yml

NOTE:

In windows you need tu use ";" as the separator (@louisvno)

Disjoined answered 6/1, 2017 at 15:37 Comment(3)
Brilliant. Sometimes the most obvious solutions are so close that one cannot see them. Thank you. It works like charm.Aphaeresis
So I´ve been literally pulling my hair until I saw this answer. GreatBelda
just a tip for the windows ppl, the separator is ; instead of :Therese

© 2022 - 2024 — McMap. All rights reserved.