docker compose orphan containers warning

Bogdan  Dubyk picture Bogdan Dubyk · Jun 20, 2018 · Viewed 43.8k times · Source

How to be with orphan images when you have 2 independent projects and you want them to work at the same time or at least to build running docker-compose up -d without --remove-orphans flag when images are already built for another project.

docker compose file1:

version: '2'
services:
  applications:
    image: tianon/true
    volumes:
      - ../../:/var/www/vhosts/project1
  nginx:
    build: ./images/nginx
    image: project1/nginx:latest
    ports:
      - "80:80"
    volumes_from:
      -  applications
    networks:
      appnet:
        aliases:
          - project1.app
          - admin.project1.app
  php:
    image: project1/php:latest
    ports:
      - "7778:7778"
    build: 
      context: ./images/php
      dockerfile: Dockerfile
    volumes_from:
      -  applications
    networks:
      -  appnet
  mysql:
    image: project1/mysql:latest
    build: ./images/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      -  mysqldata:/var/lib/mysql
    networks:
      -  appnet
    ports:
      - "33066:3306"
 workspace:
    image: project1/workspace:latest
    build:
      context: ./images/workspace
    volumes_from:
      - applications
    working_dir: /var/www/vhosts/project1
    networks:
      -  appnet
networks:
  appnet:
    driver: "bridge"
volumes:
   mysqldata:
    driver: "local"

the second docker compose file:

version: '2'
services:
  project2_applications:
    image: tianon/true
    volumes:
      - ../../:/var/www/vhosts/project2
  project2_nginx:
    build: ./images/nginx
    image: project2/nginx:latest
    ports:
      - "8080:80"
    volumes_from:
      -  project2_applications
    networks:
      project2_appnet:
        aliases:
          - project2.app
          - admin.project2.app
  project2_php:
    image: project2/php:latest
    ports:
      - "7777:7777"
    build: 
      context: ./images/php
      dockerfile: Dockerfile
    volumes_from:
      -  project2_applications
    networks:
      -  project2_appnet
  project2_mysql:
    image: project2/mysql:latest
    build: ./images/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      -  project2_mysqldata:/var/lib/mysql
    networks:
      -  project2_appnet
    ports:
      - "33067:3306"
  project2_workspace:
    image: project2/workspace:latest
    build:
      context: ./images/workspace
    volumes_from:
      - project2_applications
    working_dir: /var/www/vhosts/videosite
    networks:
      -  project2_appnet
networks:
  project2_appnet:
    driver: "bridge"
volumes:
   project2_mysqldata:
    driver: "local"

And now when I have already built project1 and trying to run docker-compose up -d for the second project I see warning:

WARNING: Found orphan containers (docker_workspace_1, docker_nginx_1, docker_php_1, docker_mysql_1, docker_memcached_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.

I have a supposition that it's because container names for project1 should be more specific and I need to add some prefixes like I'm doing for project2, but project1 is in use by many other developers and I do not want to change it.

Is there any way to turn off orphan check?

And the second thing: is just a warning message but for some reason, after it appearing compose is failing with error:

ERROR: Encountered errors while bringing up the project.

And to make it work I need to run docker-compose up -d --remove-orphans

Answer

Eugene Yarmash picture Eugene Yarmash · Dec 4, 2019

Compose uses the project name (which defaults to the basename of the project directory, but can be specified explicitly) internally to isolate projects from each other. The project name is used to create unique identifiers for all of the project's containers and other resources. For example, if your project name is myapp and it includes two services db and web, then Compose starts containers named myapp_db_1 and myapp_web_1 respectively.

You get the "Found orphan containers" warning because docker-compose detects some containers which belong to another project with the same name.

To prevent different projects from interfering with each other (and suppress the warning) you can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable. The environment variable can also be set via an environment file (.env in the current working directory by default).