Skip to content

How to Use docker exec and docker attach (When to Use Each)

DodaTech 2 min read

In this tutorial, you'll learn about How to Use docker exec and docker attach (When to Use Each). We cover key concepts, practical examples, and best practices.

The Problem

You need to run a command inside a running container. docker exec and docker attach both connect to containers, but they serve different purposes. Using the wrong one leads to broken terminals, detached processes, or accidentally killing the container. docker attach connects to the container's main process (PID 1) — pressing Ctrl+C while attached sends SIGTERM to it, which may stop the container. docker exec starts a new process inside the container, so Ctrl+C only affects that process. Choosing the right one depends on whether you need to interact with the running application or debug the container environment.

Quick Fix

1. Use docker exec to run a command inside a running container

docker exec -it my-container bash

This starts a new bash shell inside my-container. Exit with exit or Ctrl+D — the container keeps running. Use this for debugging, inspecting files, or running ad-hoc commands.

2. Use docker attach to connect to the container's main process

docker attach my-container

This attaches your terminal to the container's stdout/stdin. For a web server (NGINX, Flask), you'll see the access logs stream by. Press Ctrl+C to detach — this sends SIGTERM to the main process, which may stop the container.

3. Detach safely without stopping the container

# Detach from attach without killing the process
Ctrl+P, Ctrl+Q

This detaches your terminal from the container's main process without sending a signal. The container continues running normally.

4. Run a single command and exit with exec

docker exec my-container ls -la /app

Expected output:

total 24
drwxr-xr-x 2 root root 4096 Jan 15 10:00 .
drwxr-xr-x 2 root root 4096 Jan 15 10:00 ..
-rw-r--r-- 1 root root  123 Jan 15 10:00 app.py

No -it flag needed for non-interactive commands. The output goes to your terminal, then the command exits.

5. Use exec with environment variables

docker exec -e DB_HOST=localhost my-container env

Expected output:

PATH=/usr/local/bin:...
DB_HOST=localhost

The -e flag sets environment variables for the executed command only, without affecting the running container.

6. When to use each

# Debug a container — use exec
docker exec -it my-container sh

# View running process logs in real time — use attach
docker attach my-container

# Check container files — use exec
docker exec my-container cat /etc/nginx/nginx.conf

# Send input to a background process — use attach (echo "reload" | docker attach ...)

Prevention

  • Use docker exec -it for interactive debugging and running commands
  • Use docker attach only when you need to see the main process output (stdout/stderr)
  • Press Ctrl+P, Ctrl+Q to detach from attach without stopping the container
  • Use docker logs -f instead of attach to follow logs without risking the container
  • Never use docker attach on production containers — use docker exec or docker logs
  • Remember: exec creates a new process, attach connects to the existing main process

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro