basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Removed a service from Compose, yet its container stays: --remove-orphans

    Rename or remove a service in Compose and its old container keeps running and holding its port: Compose manages only what is in the file. docker compose up -d --remove-orphans removes those containers.

    Where orphan containers come from

    Compose finds its containers by the project label and maps them to services using the Compose file. Rename service shop to web: to Compose, web is a new service, while shop-shop-1 is left without a definition — an orphan container. up -d leaves it alone and prints a warning:

    Found orphan containers (shop-shop-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.

    If the old and new services share a port, the new one fails to start:

    Bind for 0.0.0.0:8081 failed: port is already allocated

    Without a shared port there’s no error at all: both copies keep running, the old one with its old config. It shows up in docker compose ps while missing from the Compose file.

    How to clean them up

    docker compose up -d --remove-orphans

    Compose stops and deletes the orphan containers, then starts the new setup. down accepts the same flag. To avoid having to remember it, add COMPOSE_REMOVE_ORPHANS=true to the stack’s .env — Compose reads it there and cleans up orphans on every up.

    Before deleting an orphan container, check that it doesn’t hold data outside a volume: run docker diff shop-shop-1. Lines marked A and C disappear with the container (Docker, “What the container wrote to itself”).

    If it doesn’t work

    docker compose down exited without an error, yet a container is still running and the network wasn't removed: Resource is still in use

    Cause
    It's an orphan container: down without the flag stops only services in the file, while the leftover container still holds the network. Exit code stays 0.
    Fix
    docker compose down --remove-orphans.

    The new service won't start: port is already allocated, with no orphan container warning

    Cause
    .env or the shell environment sets COMPOSE_IGNORE_ORPHANS=true: it hides the warning, while the old container still holds the port.
    Fix
    Spot the old container in docker compose ps, remove it with up -d --remove-orphans, and delete the variable.

    How to check

    List services that have containers but no definition in the Compose file:

    docker compose ps -a --format '{{.Service}}' | sort -u | grep -vxF -f <(docker compose --profile '*' config --services)
    # shop

    Empty output means no orphan containers. --profile '*' keeps running profile services (Compose profiles) out of the list: without that flag, Compose ignores them when listing services from the file.