Interactive, detached and exec

Beginner · 10 min

What -i, -t and -d actually change, how attach differs from exec, how to leave a container without stopping it, and getting a shell into a running one.

Before thisdocker run and the container lifecycle

TechWorld with Nana · 10 min

Interactive, detached and exec

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TechWorld with NanaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • -i keeps the container's stdin open. -t gives it a pseudo-terminal. You want both for a shell and neither for a server or a pipeline.
  • -d starts the container in the background and prints its ID. The process inside is the same either way. Only your terminal's connection to it changes.
  • docker attach connects your terminal to PID 1's own streams. Ctrl-C there can stop the container, and Ctrl-P Ctrl-Q detaches only if it was started with -it.
  • docker exec starts a new process inside a running container's namespaces. Exiting it leaves the container running.
  • An exec'd process isn't PID 1, isn't restarted with the container and needs the program to exist in the image. A minimal image may have no shell at all.
  • A TTY changes the output. It merges stderr into stdout and turns each newline into carriage return plus newline, which is why logs from -t containers look odd in files.

Three ways to be connected

You can run a container so it takes over your terminal, so it runs in the background and gives your terminal back, or you can step inside one that’s already running. Each is one or two letters on the command line, and getting them wrong causes the classic beginner bugs: the shell that exits instantly, the server that dies when you close the laptop lid, and the Ctrl-C that stopped production when you only meant to stop watching.

-i, -t and -d

The three flags are independent:

  • -i (--interactive) keeps the container’s stdin open and connected to yours. Without it, the process sees end-of-file on stdin straight away.
  • -t (--tty) allocates a pseudo-terminal for the container, so programs see a terminal: shells show a prompt, passwd hides what you type, and tools turn on colour.
  • -d (--detach) returns your terminal as soon as the container starts, printing its ID.

Think of a phone call. -i is your microphone being on, -t is it being a proper phone line rather than a walkie-talkie, and -d is hanging up while the other side keeps working.

Combinations from the Docker reference:

docker run -it debian                                 # interactive shell
echo hello | docker run --rm -i busybox cat           # pipe data in: -i, no -t
docker run -d --name web -p 8080:80 nginx:1.31        # a server in the background
docker run -dit --name box alpine sh                  # shell kept alive in the background

The last one matters: alpine’s command is sh, which exits as soon as its stdin closes. -dit keeps stdin open in the background so the container stays up for you to exec into or attach to later.

A TTY changes the bytes

-t isn’t cosmetic. A terminal driver sits between the process and you, and it rewrites output. I ran the same command through a pipe and through a pseudo-terminal with Python’s pty module on this Mac:

without a TTY: b'line1\nline2\nstdout is not a TTY\n'
with a TTY:    b'line1\r\nline2\r\nstdout is a TTY\r\n'

Two things changed: every \n became \r\n, and the program noticed it had a terminal. In a container started with -t, stdout and stderr also become one stream, since a terminal has only one output. So docker logs can’t separate them, and log files gain carriage returns. Servers don’t need -t, so don’t give it to them.

attach versus exec

docker attach web connects your terminal to PID 1’s own stdin and stdout. It’s the same stream you’d have had without -d. It’s risky: with --sig-proxy on (the default), Ctrl-C sends SIGINT to PID 1, which usually ends the container. Ctrl-P Ctrl-Q detaches and leaves it running, but only if the container has a TTY and stdin, meaning it was started with -it. --detach-keys changes the sequence. To watch output, use docker logs -f instead.

docker exec starts a new process inside the running container. It joins the container’s existing namespaces and cgroup through the setns system call, the same move nsenter makes in the Linux namespaces lesson:

docker exec -it web sh                         # a shell beside nginx
docker exec web nginx -t                       # run one check and return
docker exec -u 0 -w /etc/nginx web ls          # as root, in a chosen directory
docker exec -e DEBUG=1 web env                 # extra env for this process only

Exiting the exec’d shell leaves the container running, because your shell was never PID 1. Per the exec reference, the command must be an executable, so pipelines need sh -c "...". It runs only while PID 1 runs, and it isn’t restarted when the container restarts.

When there’s no shell

exec can only run programs that exist in the image. Minimal and distroless images often have no sh, which is part of why they’re chosen. Then docker exec -it api sh fails with an “executable file not found” error. You have three ways in:

  • docker debug api, which attaches its own toolbox with a shell and common tools to the container without changing the image.
  • A sidecar sharing the namespaces: docker run -it --rm --pid container:api --network container:api busybox.
  • From a Linux host, nsenter with the host’s own tools.

Debugging a running container works through these. And whatever you change by hand inside a container lives in its writable layer. It survives a restart and vanishes when the container is recreated, so a fix made through exec is a diagnosis, not a deployment.

About the video

The embed is all of TechWorld with Nana’s “Debugging Docker Containers with docker exec and docker logs”, October 2019, 10 minutes. Its timestamps put docker logs at 1:56 and docker exec -it at 5:28: watching a container’s output, then getting a terminal inside a running one. It’s from 2019, so it predates docker debug, and it doesn’t cover -i and -t separately, attach or the detach keys, which the lesson adds. English was judged from the channel and its English caption track. Shubham gour’s video in deeper compares exec and attach side by side.

What was run: the TTY comparison ran with Python’s pty module and sh on macOS, where the terminal driver’s newline translation matches Linux’s. No Docker commands ran. The flags and their behaviour come from the docker run, exec and attach references and the docker debug reference, checked 2026-11-28.

Where this fits

Next, the usual way to hand a container its settings: environment variables.

Check yourself

  1. 01You ran docker attach to watch a container's output and pressed Ctrl-C. The container stopped. Why, and what should you have done?

    attach connects to PID 1 and forwards the signal (SIGINT), which ended it. Use docker logs -f to watch output, or Ctrl-P Ctrl-Q to detach if the container was started with -it.

  2. 02Why does echo hello | docker run --rm -t busybox cat print nothing, while the same command with -i prints hello?

    Without -i, the container's stdin is closed, so cat reads end-of-file at once and never sees the piped input. -i keeps stdin open. -t isn't needed for a pipe, and it changes the output with carriage returns.

  3. 03docker exec -it api bash says the executable isn't found. The app is running fine. What does that tell you?

    The image has no bash, perhaps only sh, or no shell at all if it's distroless. exec can only run programs that exist in the container's filesystem. Try sh, or use docker debug or a sidecar container.

  4. 04You fixed a config file through docker exec and restarted the container. Is the fix still there?

    Yes, after a restart, because the writable layer survives. It's gone once the container is removed and recreated from the image, so the real fix belongs in the image or a mounted file.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark