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.
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`.
First, verify the container's current state to confirm it is stopped:
docker ps -aLook 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.
Start the stopped container first, then attach to it:
# 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.
For containers that run interactive shells (like those started with docker run -it), combine start and attach with the -ai flags:
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.
If the container keeps stopping, check the logs to understand why:
# 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
If you want to run commands in a container rather than attach to its main process, use docker exec:
# 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/bashUnlike 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.
To prevent containers from stopping immediately, ensure they have a long-running foreground process:
# 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_containerFor shell-based images, use the -it flags:
docker run -dit --name my_container ubuntu
docker attach my_containerUnderstanding 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:
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:
docker logs <container_name_or_id>Accessing stopped container filesystems: If you only need to inspect files in a stopped container:
# 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/bashMultiple 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.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec