service "web" is not running — docker compose exec runs a command
inside a running container, and the service's container has already exited. Start the
service so it stays up, or use docker compose run --rm to get a fresh
container for one command.
docker compose ps -a shows the state; an Exited status explains
the message. For a one-off command, docker compose run --rm web ... worked
on the same file.
The error
PS> docker compose -p fix10-cs-q6 exec web sh
service "web" is not running
The command exited with code 1. It came straight after a successful
docker compose up of the same project, which printed
web started, so the service name was right and the container existed. The
file:
services:
web:
image: alpine
command: ["echo", "web started"]
Why it happens
The service's command is echo web started. docker compose up
created the container, it printed its line and exited with code 0, so by the time
exec ran there was no running container to enter:
PS> docker compose -p fix10-cs-q6 ps -a
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
fix10-cs-q6-web-1 alpine "echo 'web started'" web 2 seconds ago Exited (0) Less than a second ago
The (0) after Exited is the exit code; the command ended
normally. Note that the message does not tell a stopped service from a wrong name. In a
second run, a service name that is not in the file gave the same wording:
PS> docker compose -p fix10-cs-q6 exec nosuch sh
service "nosuch" is not running
So check the name against the file, then ps -a for the state.
The fix
For one command in a new container of the same service:
PS> docker compose -p fix10-cs-q6 run --rm web sh -c "echo inside the container"
Container fix10-cs-q6-web-run-996569e60b5d Creating
Container fix10-cs-q6-web-run-996569e60b5d Created
inside the container
run created a container named web-run-... from the service's
settings and ran the command in it; the docker compose down -v that followed
removed only fix10-cs-q6-web-1, so the run container was
already gone. For exec itself, the service's container has to be running
when you call it; for this service ps -a printed
"Created 2 seconds ago" and "Exited (0) Less than a second ago".
How it was reproduced
One alpine service running echo web started, started with
docker compose up from Windows PowerShell 5.1, followed by
docker compose exec web sh, ps -a and
run --rm web sh -c "echo inside the container"; in a second run of the same
project, up followed by exec nosuch sh. Docker Compose v5.7.0,
Docker Engine 29.9.3, Docker Desktop 4.99.0 on Windows (version 10.0.26300). The project
name fix10-cs-q6 is this test's own; it was removed with
docker compose down -v.
Frequently asked
- What does 'service is not running' mean in docker compose exec?
- In this test the service's container had exited, so there was nothing running to execute a command in, and docker compose ps -a showed its Exited status. A service name that is not in the file gave the same message, so check the name too.
- How do I run a command in a compose service that has exited?
- Use docker compose run --rm followed by the service name and the command. In this test it created a new container from the service, ran the command and removed the container.
- Why does my compose container exit right away?
- Its main command finished. In this test the command was echo, which printed one line and exited with code 0, so exec found no running container afterwards.
More decoded errors in the Fixes category. For the plain Docker command, see Docker: container is not running from docker exec.