This warning appears when Docker Compose detects containers that belong to the same project but are no longer defined in your docker-compose.yml file. It typically happens after renaming or removing services, or when multiple compose files share the same project name.
When you run Docker Compose commands, it identifies containers using a project name, which by default is the name of the directory containing your `docker-compose.yml` file. This project name gets assigned to containers as the `com.docker.compose.project` label. The "Found orphan containers" warning appears when Docker Compose finds containers with a matching project label that aren't defined in the current `docker-compose.yml` file. This commonly happens in several scenarios: - **Removed services**: You deleted a service from your compose file, but its container still exists - **Renamed services**: You changed a service name, creating a new container while the old one remains - **Same directory name**: Two different projects happen to be in directories with the same name, causing their containers to share a project name - **Multiple compose files**: Running different compose files from the same directory without specifying unique project names This is a warning, not an error - your containers will still start normally. However, orphan containers consume resources and can cause confusion during debugging.
The simplest solution is to let Docker Compose remove the orphan containers automatically:
# Remove orphans when starting services
docker-compose up -d --remove-orphans
# Or remove orphans when stopping services
docker-compose down --remove-orphansThis tells Docker Compose to stop and remove any containers that belong to this project but aren't defined in the current compose file.
If you have multiple projects or compose files that might conflict, give each a unique project name. There are three ways to do this:
Option 1: Use the -p flag
docker-compose -p my-unique-project up -dOption 2: Set COMPOSE_PROJECT_NAME in a .env file
Create a .env file in the same directory as your docker-compose.yml:
COMPOSE_PROJECT_NAME=my-unique-projectOption 3: Add a top-level name in docker-compose.yml
name: my-unique-project
services:
web:
image: nginx
# ... other servicesThis ensures different projects don't interfere with each other even if they're in directories with the same name.
If the orphan containers are expected and you don't want to remove them, you can suppress the warning:
Set environment variable for a single command:
COMPOSE_IGNORE_ORPHANS=true docker-compose up -dOr add to your .env file for permanent effect:
COMPOSE_IGNORE_ORPHANS=trueUse this option when you intentionally have containers from different compose files that you want to keep running.
If you want to inspect and selectively remove orphan containers:
# List all containers including stopped ones
docker ps -a
# Find containers belonging to the current project
docker ps -a --filter "label=com.docker.compose.project=$(basename $PWD)"
# Remove specific containers manually
docker rm -f <container_name_or_id>This gives you more control if you only want to remove certain orphaned containers.
When working with multiple compose files (e.g., docker-compose.yml and docker-compose.test.yml), specify all files you want to use:
# Explicitly use multiple compose files
docker-compose -f docker-compose.yml -f docker-compose.test.yml up -dOr set the COMPOSE_FILE environment variable in your .env file:
COMPOSE_FILE=docker-compose.yml:docker-compose.test.ymlThis tells Docker Compose about all the services across both files, preventing false orphan warnings.
Understanding project names: Docker Compose uses the project name as a namespace for all resources (containers, networks, volumes). The default project name is the directory name, converted to lowercase with special characters removed. You can verify the current project name with:
docker-compose config | grep -i nameWhy --remove-orphans doesn't work with some commands: Commands like docker-compose run and docker-compose create may suggest using --remove-orphans, but they don't actually accept this flag. In these cases, run docker-compose down --remove-orphans first, then execute your command.
Complete cleanup: For a thorough cleanup including volumes and images:
docker-compose down --volumes --rmi all --remove-orphansCI/CD considerations: In automated pipelines, always use explicit project names and --remove-orphans to ensure clean state:
docker-compose -p ci-build-${BUILD_NUMBER} up -d --remove-orphans
# ... run tests ...
docker-compose -p ci-build-${BUILD_NUMBER} down --volumes --remove-orphansdockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Container exited with code 128: invalid exit argument
How to fix 'Container exited with code 128' in Docker
Error response from daemon: Get https://registry-1.docker.io/v2/: Proxy Authentication Required
How to fix 'Proxy Authentication Required' in Docker
error exporting cache: failed to export cache
How to fix 'error exporting cache: failed to export cache' in Docker
net/http: TLS handshake timeout
How to fix 'net/http: TLS handshake timeout' in Docker