What this error means
The message "No container found for <service>_1" appears when docker compose exec cannot locate a running container for the service you named. Common causes:
- The service is not running (stopped or never started).
- You started containers under a different project name, directory, or compose file set.
- Profiles or scaling changed which containers exist.
- You mixed
docker compose(v2) anddocker-compose(v1) or different flags.
exec only works against running containers created by the same Compose project context.
Minimal working example
Compose file:
# docker-compose.yml
services:
app:
image: alpine:3.19
command: ["sleep", "infinity"]
Start and exec:
# Start in detached mode
docker compose up -d
# Should work: opens a shell in the running container
docker compose exec app sh
# Reproduce the error: stop the service
docker compose stop app
# Now exec fails
docker compose exec app sh
# -> Error: No container found for app_1
Fix: start the service again:
docker compose up -d app
docker compose exec app sh
Quickstart: fix in 30 seconds
- Ensure the service is running:
docker compose psthendocker compose up -d <service>. - Run from the same directory and files used to start:
cd <project-dir>and pass the same-ffiles and-pif used. - If scaled:
docker compose exec --index 1 <service> sh. - If profiles were used: add
--profile <name>or setCOMPOSE_PROFILES. - If the container exits immediately, use
docker compose run --rm <service> shor adjust the service command to stay running.
Step-by-step fixes
- Verify the service is running
docker compose ps
# or restrict to running
docker compose ps --status running
- If your service is not listed as running, start it:
docker compose up -d <service>
- Make sure you are in the correct project context
- Default project name is the directory name. If you started in a different directory,
execwon’t find the containers. - Use the same compose files and project name you used with
up:
# If you used multiple files previously, pass the same set and order
docker compose -f docker-compose.yml -f docker-compose.override.yml exec <service> sh
# If you used a custom project name earlier
docker compose -p myproj exec <service> sh
- Not sure which project your containers belong to? Inspect labels:
docker ps --filter label=com.docker.compose.service=<service> \
--format '{{.Names}} {{.Label "com.docker.compose.project"}}'
- Align Compose CLI versions
- If you started with
docker-compose up(v1), prefer using the same CLI forexec. Conversely, if you useddocker compose(v2), keep using it. - Check your version:
docker compose version
# and/or
docker-compose version
- Handle profiles and conditional services
- If your service is gated by a profile, it won’t exist unless the profile is enabled:
# Enable profile when starting or exec-ing
COMPOSE_PROFILES=dev docker compose up -d
COMPOSE_PROFILES=dev docker compose exec <service> sh
# or
docker compose --profile dev up -d
docker compose --profile dev exec <service> sh
- Address scaling and replicas
- If you scaled a service, there may be multiple containers. Pick one by index:
docker compose up -d --scale web=3
# Exec into the first replica
docker compose exec --index 1 web sh
# Or the third replica
docker compose exec --index 3 web sh
- When the container exits immediately
execrequires a running container. If the service command finishes quickly, either:- Keep it running (e.g., use
sleep infinity, a long-running process, or your app server), or - Use a one-off container:
- Keep it running (e.g., use
# Run an ad-hoc shell without needing the service to be running
docker compose run --rm --no-deps <service> sh
- Match the exact compose files used previously
- Starting with different
-ffiles creates a different project definition;execwon’t target the earlier containers. - Always pass the same files, in the same order, for
up,ps, andexec.
- Clean up orphans and stale state
# Remove stopped containers and dangling project state
docker compose rm -f
# Recreate containers to ensure labels and names are consistent
docker compose up -d --force-recreate
Common pitfalls
- Running
execbeforeup -dcreates any containers. - Changing directories between
upandexec(thus changing the default project name). - Forgetting
-p <name>onexecwhen it was used onup. - Using a different set or order of
-fcompose files between runs. - Expecting
execto work with services that exit immediately. - Scaling a service but forgetting to target a specific replica with
--index. - Defining
container_name:and then attempting to scale the service (scaling is incompatible with a fixedcontainer_name).
Performance notes
docker compose execattaches to an existing running container; it’s fast and does not create new containers.docker compose runstarts a new one-off container; it’s slower and consumes extra resources until it exits.- Keeping a lightweight long-running process (e.g., your app server or
sleep infinityin dev) avoids repeated container startups. - Frequent
execis fine; the overhead is mostly an interactive TTY attach.
Tiny FAQ
Why does Compose mention
<service>_1?- Compose indexes replicas. The first instance is index 1. Names differ between versions, but the concept is the same.
How do I exec into a specific replica?
- Use
docker compose exec --index N <service> <cmd>.
- Use
Can I exec into a stopped container?
- No. Start it with
docker compose up -d <service>or usedocker compose runfor a one-off container.
- No. Start it with
I used a different directory or project name. How do I recover?
- Use the same directory or add
-p <project>tops/up/execto match the original project.
- Use the same directory or add
Does
container_name:affect this?- It prevents scaling and fixes the container name, but
execstill targets the service via labels. Mismatched scaling or differing project context can still cause the error.
- It prevents scaling and fixes the container name, but