Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

docker compose down finishes, yet some containers are still there

Your error

docker compose down returns at once, but docker ps still lists the stack's containers1 more line it can show

Fix it fast

Most common cause, 1 of 3

down runs under a different project name than up did

Run down with the project name the stack really has, then pin one name in the file so every command agrees.

Fix
docker compose -p shop-api down

# compose.yaml
name: shop-api
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

docker compose down removes only the containers of services that the current project and its active profiles define. Containers survive when down runs under another project name (other folder, -p or COMPOSE_PROJECT_NAME), when their service sits in a profile you did not pass, or when the service left the file. Match the project, add --profile, or use --remove-orphans.

  • Applies to Docker Compose v5.8 (docker compose plugin), Docker Engine 29.10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

From the folder you ran down in, compare what this project sees with what Docker runs. A container that docker ps shows but docker compose ps does not belongs to another project, another profile or a removed service.

Run this first
docker compose ls --all
docker compose ps --all
docker ps --format '{{.Names}}'

Then find the line you see in this list and open its fix:

  1. 1down runs under a different project name than up diddocker compose ps --all is empty here, while docker compose ls --all lists a running project under another name
  2. 2The leftover service belongs to a profile that down was not givenThe container that stays up comes from a service with a profiles: key, started earlier with --profile or COMPOSE_PROFILES
  3. 3The service was renamed or deleted from the Compose fileThe surviving container was created for a service that docker compose config --services no longer lists

What does the error look like?

  • docker compose down returns at once, but docker ps still lists the stack's containers
  • One service of the stack, such as an admin or debug tool, keeps running after docker compose down

Cause 1 of 3

down runs under a different project name than up did

You seedocker compose ps --all is empty here, while docker compose ls --all lists a running project under another name

Check

Compose keeps projects apart by name, and docker compose down only touches the project it resolves. That name comes from -p first, then COMPOSE_PROJECT_NAME, then the top-level name: attribute, then the base name of the folder holding the Compose file. Starting the stack with -p, from a copy of the folder, or with the variable set, and stopping it without them, points down at an empty project.

Check
grep -n '^name:' compose.yaml
echo "COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME}"
docker compose ls --all

Fix

Run down with the project name the stack really has, then pin one name in the file so every command agrees.

Fix
docker compose -p shop-api down

# compose.yaml
name: shop-api

Verify

docker compose ls --all no longer lists the project, and docker ps shows none of its containers.

Verify
docker compose ls --all

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

The leftover service belongs to a profile that down was not given

You seeThe container that stays up comes from a service with a profiles: key, started earlier with --profile or COMPOSE_PROFILES

Check

Services without a profiles attribute are always enabled, but a service assigned to a profile is included only when that profile is active. So a plain docker compose down stops the unprofiled services and leaves running whatever you started with --profile debug.

Check
grep -n -B3 'profiles:' compose.yaml

Fix

Pass the same profile to down that you passed to up, either as a flag or through the environment variable.

Fix
docker compose --profile debug down
# or
COMPOSE_PROFILES=debug docker compose down

Verify

docker compose ps --all with the same profile lists nothing.

Verify
docker compose --profile debug ps --all

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

The service was renamed or deleted from the Compose file

You seeThe surviving container was created for a service that docker compose config --services no longer lists

Check

By default down removes containers for the services the file defines now. A container created for a service you later renamed or deleted is no longer one of them, so it outlives every plain down.

Check
docker compose config --services
docker ps --format '{{.Names}}'

Fix

Ask down to take the leftover containers with it.

Fix
docker compose down --remove-orphans

Verify

docker ps no longer shows the leftover container.

Verify
docker ps --format '{{.Names}}'

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Put a top-level name: in every Compose file, so the folder name, -p and COMPOSE_PROJECT_NAME cannot make up and down disagree.
  • Set COMPOSE_PROFILES once for the whole deploy script, so up and down always see the same profiles instead of one command getting --profile and the other not.
  • Run docker compose down --remove-orphans in deploy scripts after any change that renames or drops a service.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.