✦ For everyone, free.

Practical knowledge for real and everyday life

Home

6.2 Container Run Modes

A focused guide to Container Run Modes, connecting core concepts with practical Docker and container operations.

Container run modes are the different ways docker run can attach (or not attach) to a container's main process, determining whether the terminal stays connected to the container's output and input, or whether the container runs independently in the background.

Detached Mode

The -d flag runs a container in the background, immediately returning control of the terminal without attaching to the container's output.

docker run -d myapp:1.0

This is the typical mode for long-running services, where the terminal that launched the container doesn't need to remain tied to it.

Foreground (Attached) Mode

Without -d, a container runs attached to the current terminal, with its output streamed directly and the terminal blocked until the container exits.

docker run myapp:1.0

This mode is useful for quickly observing a container's output directly, or for short-lived commands where waiting for completion in the foreground is the intended behavior.

Interactive Mode

The -it combination (interactive plus a pseudo-TTY) allows direct interaction with the container's main process, commonly used for shells or other applications expecting interactive input.

docker run -it ubuntu:22.04 bash
Switching Between Modes for the Same Container

A container started in detached mode can still have its output observed afterward, without needing to have been run attached in the first place.

docker run -d --name myapp myapp:1.0
docker logs -f myapp

docker logs -f attaches to the container's log stream after the fact, providing similar visibility to what attached mode would have provided from the start.

Why Understanding Run Modes Matters

Choosing the appropriate run mode — detached for long-running services, interactive for shells, foreground for short commands — directly affects how a container's terminal behaves and is one of the first practical decisions made every time a container is started.

Content in this section