How to fix Error response from daemon: You cannot attach to a stopped container in Docker

DockerBEGINNERLOWUpdated Dec 6, 2025ยท 170 views

The 'docker attach' command fails because the container is not running. Start the container first with 'docker start' before attaching, or use 'docker start -ai' for interactive containers.

Quick fix

Fixes Error response from daemon: You cannot attach to a stopped container

docker ps -a
  1. Check container status
  2. Start the container before attaching
  3. Use 'docker start -ai' for interactive containers
  4. Check why the container stopped
docker โ€” error
docker ps -a
Error response from daemon: You cannot attach to a stopped container

What this error means

This error occurs when you attempt to use `docker attach` on a container that is not currently running. The full error message reads: "Error response from daemon: You cannot attach to a stopped container, start it first". The `docker attach` command connects your terminal's standard input, output, and error streams to a running container's main process (PID 1). Since the container is stopped, there is no running process to attach to - the container's main process has either completed, crashed, or was explicitly stopped. This is different from `docker exec`, which runs a new command inside a container. Both commands require the target container to be in a running state. If you need to inspect a stopped container's filesystem or debug why it exited, you'll need to use alternative approaches like `docker start`, `docker logs`, or `docker cp`.

Typical symptoms

  • 'docker attach' fails with 'Error response from daemon: You cannot attach to a stopped container, start it first'
  • Container shows 'Exited' status when running 'docker ps -a'
  • Previously running container is now stopped and attach no longer works
  • Container that was detached from (Ctrl+P, Ctrl+Q) has since stopped
  • Interactive container exited after terminal disconnection

Common causes

  1. Container's main process (CMD/ENTRYPOINT) completed or exited
  2. Container was explicitly stopped with 'docker stop' command
  3. Container crashed due to application error
  4. Container was started without interactive flags (-it) for shell-based images
  5. Container was killed by the OOM killer or resource constraints
  6. Container had no long-running foreground process to keep it alive

How to fix "Error response from daemon: You cannot attach to a stopped container"

1Check container status

First, verify the container's current state to confirm it is stopped:

bash
docker ps -a

Look for your container in the output. The STATUS column will show:
- "Exited (0)" - Process completed successfully
- "Exited (1)" or other non-zero - Process crashed or encountered error
- "Created" - Container was created but never started

Note the container name or ID for the following steps.

2Start the container before attaching

Start the stopped container first, then attach to it:

bash
# Start the container
docker start <container_name_or_id>

# Now attach to the running container
docker attach <container_name_or_id>

Important: If the container exits immediately after starting, the main process is completing too quickly. See the next steps for solutions.

3Use 'docker start -ai' for interactive containers

For containers that run interactive shells (like those started with docker run -it), combine start and attach with the -ai flags:

bash
docker start -ai <container_name_or_id>

The flags mean:
- -a (attach) - Attach STDOUT/STDERR and forward signals
- -i (interactive) - Attach container's STDIN

This is equivalent to starting and then attaching, but ensures the interactive session works correctly.

4Check why the container stopped

If the container keeps stopping, check the logs to understand why:

bash
# View container logs
docker logs <container_name_or_id>

# View more detailed container info
docker inspect <container_name_or_id> --format='{{.State.ExitCode}}'

Common exit codes:
- 0: Process completed successfully (normal exit)
- 1: General error
- 137: Killed by SIGKILL (often OOM killer)
- 139: Segmentation fault

5Use docker exec instead (for running containers)

If you want to run commands in a container rather than attach to its main process, use docker exec:

bash
# First, start the container
docker start <container_name_or_id>

# Then exec into it with a shell
docker exec -it <container_name_or_id> /bin/bash

Unlike docker attach, docker exec runs a new process inside the container rather than connecting to the existing main process. This is often more useful for debugging.

6Keep containers running with persistent processes

To prevent containers from stopping immediately, ensure they have a long-running foreground process:

bash
# Run with a process that stays alive
docker run -d --name my_container <image> tail -f /dev/null

# Or use sleep infinity
docker run -d --name my_container <image> sleep infinity

# Now attach works (though these processes have no interactive output)
docker attach my_container

For shell-based images, use the -it flags:

bash
docker run -dit --name my_container ubuntu
docker attach my_container

Advanced notes

Understanding docker attach vs docker exec: The docker attach command connects your terminal to the container's main process (PID 1). Whatever the container was started with as its CMD or ENTRYPOINT is what you'll be attached to. In contrast, docker exec starts a new process inside the container.

Detaching without stopping: When attached to a container, use the escape sequence Ctrl+P, Ctrl+Q to detach without stopping the container. Using Ctrl+C will send SIGINT to the main process, which may cause it to exit (stopping the container).

Paused containers: If you receive "You cannot attach to a paused container, unpause it first", the container is paused (suspended), not stopped. Use:

bash
docker unpause <container_name_or_id>
docker attach <container_name_or_id>

Restarting containers: If you receive "You cannot attach to a restarting container, wait until it is running", the container is in the process of restarting. Wait a moment and try again, or check why it's in a restart loop:

bash
docker logs <container_name_or_id>

Accessing stopped container filesystems: If you only need to inspect files in a stopped container:

bash
# Copy files out of stopped container
docker cp <container_id>:/path/to/file ./local_destination

# Or create a new image from the stopped container
docker commit <container_id> debug-image
docker run -it debug-image /bin/bash

Multiple attach sessions: You can attach multiple terminals to the same running container simultaneously. All terminals will see the same output and can send input to the same process.

Frequently asked questions

What does "Error response from daemon: You cannot attach to a stopped container" mean?
This error occurs when you attempt to use docker attach on a container that is not currently running. The full error message reads: "Error response from daemon: You cannot attach to a stopped container, start it first". The docker attach command connects your terminal's standard input, output, and error streams to a running container's main process (PID 1).
How do I fix "Error response from daemon: You cannot attach to a stopped container"?
The 'docker attach' command fails because the container is not running. Start the container first with 'docker start' before attaching, or use 'docker start -ai' for interactive containers.
What causes "Error response from daemon: You cannot attach to a stopped container"?
Container's main process (CMD/ENTRYPOINT) completed or exited Container was explicitly stopped with 'docker stop' command Container crashed due to application error
Is the "Error response from daemon: You cannot attach to a stopped container" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading