97

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

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Bogdan Dubyk
  • 4,756
  • 7
  • 30
  • 67
  • I also have this issue with two different users in the system – Kostanos Aug 30 '21 at 09:45
  • To split two unrelated stacks located in folder named similar, eg. proj1/x_name, proj2/x_name. You can now name them specifically vial top level `name: proj_name`: https://docs.docker.com/compose/compose-file/#name-top-level-element – Eugen Konkov Oct 29 '22 at 10:40

9 Answers9

101

Compose uses the project name (which defaults to the basename of the project directory) 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 any of the following options:

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 34
    There are times when I really hate Docker documentation for omitting important details like these. I've spent way too much time trying to figure out why `docker-compose` treats two completely separate YML files as part of the same project and even came to the conclusion myself that it must be tied to the project name. +1 –  Feb 11 '20 at 13:44
  • 6
    My two separate projects are in directories `C:\somedir1\docker` and `C:\somedir2\docker`. But I think docker-compose gets as project name just the enclosing folder (`docker`), not the whole path. So, setting the project name for docker-compose is mandatory fo rmy case. – Emre Tapcı Apr 30 '21 at 12:20
  • why project name cant be specified in `docker-compose.yml` itself? – marko kraljevic May 16 '22 at 08:59
  • 1
    @markokraljevic since there can be multiple files via `-f` option, what would it use if they all had a "project" in them? – escape-llc Jul 14 '22 at 14:58
  • Check @user626201 [solution](https://stackoverflow.com/a/63539983/9618184) on how to setup the `COMPOSE_PROJECT_NAME` environment variable. – Binar Web Jul 25 '22 at 15:34
  • 1
    @markokraljevic: Now you can: https://docs.docker.com/compose/compose-file/#name-top-level-element – Eugen Konkov Oct 29 '22 at 10:38
56

docker-compose takes the name of the directory it is in as the default project name.

You can set a different project name by using -p or --project-name. https://docs.docker.com/compose/reference/#use--p-to-specify-a-project-name

I had a similar problem because my projects all had the docker/docker-compose.yml structure.

bob
  • 1,069
  • 1
  • 10
  • 18
21

To build on other answers, I create a .env file with my docker compose projects. I have a number of projects that all use the docker directory but are different projects.

To use docker-compose -p is a bit error prone, so creating .env file in the same directory as the docker-compose.yml:

-rw-rw-r--  1 auser auser 1692 Aug 22 20:34 docker-compose.yml
-rw-rw-r--  1 auser auser   31 Aug 22 20:44 .env

alleviates the necessary overhead of remembering -p.

In the .env file, I can now set the COMPOSE_PROJECT_NAME variable:

COMPOSE_PROJECT_NAME=myproject

On running:

docker-compose up -d

the COMPOSE_PROJECT_NAME is substituted without the use of -p.

Reference: https://docs.docker.com/compose/env-file/

user626201
  • 1,623
  • 3
  • 19
  • 36
19
docker-compose up --remove-orphans

you can run this command to clean orphan containers. As specified in the warning

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
User
  • 315
  • 3
  • 5
15

If the orphaned containers are expected and not intended to remove, you can set COMPOSE_IGNORE_ORPHANS variable to true.

Consise but just right away working source is here.

One option is to put it as a line into .env file next to docker-compose.yml like this:

COMPOSE_IGNORE_ORPHANS=True

Another option is pass or set it as an environment variable.

sh:

COMPOSE_IGNORE_ORPHANS=True docker-compose up -d 

or

export COMPOSE_IGNORE_ORPHANS=True
docker-compose up -d 

cmd:

SET COMPOSE_IGNORE_ORPHANS=True&& docker-compose up -d

powershell:

$env:COMPOSE_IGNORE_ORPHANS = 'True'; & docker-compose up -d
moudrick
  • 2,148
  • 1
  • 22
  • 34
3

TL;DR

You can also add a unique name: myproject to each of your compose files.

My journey

In case this helps anybody else scrounging around to find help for the above issue (This is in support of the already good comments here):

I have several config files in the same directory

redis.yml
mariadb.yml
...

and I kept getting the same error about orphan containers when I ran

docker-compose -f <one of my configs>.yml up

as of now you can simply put each yml file into a separate project. This is simply done using the command like parameter "-p my_project_name" as has already been mentioned before. BUT the name must be in all lowercase!

This got me a little closer but I also kept forgetting that to bring the docker container down using docker-compose I needed to include that parameter as well.

For example to start the container:

docker-compose -p myproject-d redis.yml up -d

and to destroy the container

docker-compose -p myproject-d redis.yml down

Today I found that I can simply add the name: bit into the yml config. Here is an example for redis:

version: '3.9'
name: redis
services:
  redis_0:
     ...

Now I can simply start the container with the following and don't have to worry about project names again:

docker-compose -f redis.yml <up/down>
AcidHawk
  • 496
  • 7
  • 18
2

This works for me and with it -d so that it runs in the background:
docker-compose up -d --remove-orphans

shades3002
  • 879
  • 9
  • 11
0

This happens when your docker-compose file has got updated. I received similar error on Docker startup and found out that another team member updated the docker-compose.yml as part of cleanup. To fix this, I deleted the docker group using the Delete button in Docker Desktop and started it again. This fixed the error for me.enter image description here

Ghanendra
  • 341
  • 2
  • 13
0

As a complement for the existing answers, if you're using docker-compose with the -f option, to my surprise docker-compose will use the name of the parent folder of the first file passed via -f as the project name.

For example, assuming the following folder structure:

/
└── Users/
    └── papb/
        ├── a.yml
        └── foo/
            └── b.yml
  • If you're in /Users and run docker-compose -f papb/a.yml -f papb/foo/b.yml:
    • The project name will be inferred as papb
    • Any relative paths you have in both files will be resolved against /Users/papb
  • If you're in /Users and run docker-compose -f papb/foo/b.yml -f papb/a.yml:
    • The project name will be inferred as foo
    • Any relative paths you have in both files will be resolved against /Users/papb/foo
  • If you're in /Users/papb and run docker-compose -f foo/b.yml -f a.yml:
    • The project name will be inferred as foo
    • Any relative paths you have in both files will be resolved against /Users/papb/foo
Pedro A
  • 3,989
  • 3
  • 32
  • 56