docker-modos-ejecucion-interactivo-detached

Interactive vs Detached Execution Modes

  • 4 min

The execution modes in Docker determine how the main process of a container interacts with the input and output streams of our console.

When you start with Docker, one of the first things that confuses you is the terminal’s behavior.

  • Sometimes you run a command and it gives back the control immediately, leaving the container running “in the background”.
  • Other times, the container hijacks your terminal, shows infinite logs, and you can’t type anything else.
  • And other times, you start a container… and it dies instantly.

All of this depends, precisely, on the execution modes. In Docker there are two main ways to run a container.

ModeFlagYour GoalExample
Detached-dLong-running services (Web, DB, APIs)docker run -d nginx
Interactive-itOne-off scripts or hopping in to tinker (Shell, REPL)docker run -it python

Let’s take a deep dive 👇

Detached Mode (-d)

This is the most common mode for web servers, databases and services that must always be running.

When we use the -d (Detached) flag, we are telling Docker to start the container in the background, print its ID, and return control of the terminal immediately.

Let’s see it with a practical example:

docker run -d -p 80:80 nginx

What happens?

  1. Docker starts Nginx.
  2. It prints a long ID (e.g., a1b2c3...).
  3. It returns the prompt so you can keep working. The container stays alive “behind the scenes”. If you close your terminal, the container keeps running.

Interactive Mode (-it)

The detached mode is great for servers, but… what if I want to enter a container? What if I want to run a Python script manually, or open an Ubuntu terminal?

If you start Ubuntu in detached mode, it will shut down instantly (we’ll explain why later 1️⃣). You need interactive mode.

The “magic flag” here is -it. It’s actually two flags together:

  • -i (Interactive): Keeps the input channel (STDIN) open. This allows the container to “listen” for what you type.
  • -t (TTY): Allocates a pseudo-terminal. It makes the output look nice with colors and a prompt, like a real terminal.

For example:

docker run -it ubuntu /bin/bash

What happens?

  1. Docker starts Ubuntu.
  2. Your prompt changes. You are no longer on your PC (e.g., C:\Users\Luis>), now it shows something like root@a1b2c3:/#.
  3. You’re inside! If you run ls, you will see the Linux files of the container, not the ones on your Windows machine.

To exit, simply type exit. When you type exit in the container’s main process, it stops. The container enters an Exited state.