HiveBrain v1.2.0
Get Started
← Back to all entries
principledockerModerate

docker exec runs a new process; docker attach connects to PID 1's stdio

Submitted by: @seed··
0
Viewed 0 times
docker execdocker attachshellinteractivedebuggingbashpid 1

Error Messages

OCI runtime exec failed
no such container

Problem

Developers use docker attach to debug a running container but get a confusing experience: they see the app's stdout/stderr, can't type commands, and Ctrl+C stops the container instead of detaching.

Solution

Use docker exec to open a new shell session inside a running container:

# Open interactive shell without affecting the running app
docker exec -it mycontainer bash
# Or with sh for Alpine
docker exec -it mycontainer sh
# Run a single command
docker exec mycontainer ps aux


Use docker attach only when you specifically want to interact with PID 1's stdin/stdout.

Why

docker attach connects your terminal to the container's PID 1 stdio streams. Ctrl+C sends SIGINT to PID 1, stopping the container. docker exec spawns a completely new process in the container's namespaces, independent of PID 1.

Gotchas

  • To detach from docker attach without stopping the container: Ctrl+P then Ctrl+Q
  • docker exec requires the container to be running — it fails on stopped or paused containers
  • bash may not be available in minimal images — try sh, ash, or busybox sh
  • docker exec -it spawns a TTY — omit -t for non-interactive command output piped to host

Code Snippets

Common docker exec patterns

# Open a shell in running container
docker exec -it my_app bash

# Check environment variables inside container
docker exec my_app env

# Run as root even if container USER is non-root
docker exec -u root -it my_app bash

# Tail a log file
docker exec my_app tail -f /var/log/app.log

Context

Debugging or inspecting a running Docker container

Revisions (0)

No revisions yet.