Table of Contents
Understanding How Containers Run
When you start a container with docker run, you can choose how closely you want to interact with it. The two most common styles are interactive mode and detached mode. They use the same images and the same underlying Docker features, but they feel very different from the perspective of someone using the terminal.
This chapter focuses on how these two modes behave, what the typical use cases are, and which command line flags influence them. Other aspects of docker run are discussed in the dedicated chapter for that command.
Foreground Containers and Your Terminal
By default, when you run a container that prints to the terminal, your shell stays attached to that container. You see anything the main process in the container writes to standard output or standard error. If that process expects input from standard input, you can type directly into the terminal, and the container receives those keystrokes.
In this situation, the container is in the foreground from the point of view of your terminal. Your shell is busy until the container’s main process exits or until you interrupt it. This is the foundation of interactive mode.
Detached mode, on the other hand, breaks this close link. The container runs in the background and your terminal becomes free immediately while the process continues to run.
Interactive Mode Explained
Interactive mode is about two things: seeing live output and sending input into the container. This is especially useful when you want to explore, debug, or run a shell inside a container.
The key options that enable interactive behavior are -i and -t.
The -i flag tells Docker to keep the standard input of the container open. Even if you do not type anything immediately, Docker keeps the input stream attached so you can interact later.
The -t flag asks Docker to allocate a pseudo terminal, often referred to as a TTY. This provides a terminal-like environment inside the container, which is useful for shells and programs that expect a real terminal, such as bash, sh, or text-based tools.
You will usually see these two flags combined as -it. This combination lets you run a container in the foreground with a terminal-like experience. For example, you can start a container and drop into an interactive shell prompt inside it, then type commands as if you were logged in to a machine.
In interactive mode, your terminal is dedicated to that container session. Signals such as Ctrl + C are sent directly to the process running inside the container. When you exit the shell or when the main process inside the container finishes, the container stops and your local shell prompt returns.
Important rule: Use -it when you want to type commands into a running container and see immediate responses, for example for debugging or manual exploration.
Detached Mode Explained
Detached mode starts a container and then immediately returns control to your terminal. The container keeps running in the background, without holding your shell open. This is ideal for long running services such as web servers, databases, or any application that should continue running while you do something else.
Detached mode is enabled with the -d flag to docker run. In this mode, Docker prints the container ID and then stops attaching your terminal to the container’s input or output. The container continues executing the main process inside it.
Even though you do not see the output directly, the container still produces logs. You can view them later through commands designed for that purpose, which are covered in the logging chapter.
Detached containers also keep running after you close your terminal session, as long as the Docker daemon continues to run. This makes them more suitable for background services than interactive sessions.
Important rule: Use -d when you want the container to run in the background as a service, without occupying your current terminal.
Combining and Comparing the Modes
You cannot meaningfully combine -d with -it in a way that behaves as both fully interactive and fully detached. If you run a container in detached mode, your terminal is not attached to its input or output, so the interactive benefits of -it are not available in the same way.
What you can do instead is choose the right pattern for each task. For quick experiments or manual commands inside a container, use interactive mode. For application services that must keep running, start them detached.
If you start in detached mode and later realize you need to inspect or interact with the container, you do not restart it. Instead, you use separate commands that let you execute a shell or single commands inside an already running container. Those commands attach temporarily without changing the mode in which the container was started and are described more fully in another chapter.
Similarly, if you start in interactive mode and you want to stop or disconnect, you simply exit the shell or interrupt the process. There is no need for special flags to detach afterward when you use docker run directly.
Key distinction: Interactive mode ties your terminal to the container process in the foreground. Detached mode runs the container as a background task and frees your terminal immediately.
Why the Mode Matters in Everyday Use
For absolute beginners, it is easy to treat containers like short lived commands and always use interactive mode. While this works for tiny experiments, it becomes impractical when you need services available all the time. In real projects, you usually mix both approaches.
During development, you might run a database in detached mode so that it is always available, while you open interactive containers occasionally to debug or inspect files. During troubleshooting, you might temporarily start an additional container interactively to test network connectivity or configuration.
The choice of mode also affects how you manage containers later. Background containers are typically stopped or removed using separate commands, because they are not tied to your terminal session. Interactive containers, by contrast, often stop as soon as you exit their main process, so you manage their lifecycle by leaving or closing the session.
Understanding this distinction early helps you avoid confusion, such as wondering why you no longer see a container after closing an interactive shell, or why a detached service seems silent when you expect live logs.
Practical Guidelines for Beginners
When you are unsure which mode to use, think about how you intend to work with the container. If you expect to type into it and treat it like a temporary environment, choose an interactive terminal. If you expect it to behave like a server that should run continuously in the background, choose detached mode.
Practical rule of thumb: Interactive for short lived manual sessions, detached for long running services.
With this mental model, you can choose the appropriate flags to docker run and avoid common surprises as you launch your first containers.