0
0
Dockerdevops~10 mins

Running containers in detached mode in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Running containers in detached mode
Start Docker Container Command
Check for -d flag?
NoRun container in foreground
Yes
Run container in detached mode
Container runs in background
Return container ID
User can continue using terminal
This flow shows how Docker decides to run a container in detached mode when the -d flag is used, allowing the container to run in the background and freeing the terminal.
Execution Sample
Docker
docker run -d nginx

docker ps
Run an nginx container in detached mode and then list running containers.
Process Table
StepCommandActionOutput/Result
1docker run -d nginxCheck for -d flagFlag found, proceed to detached mode
2docker run -d nginxStart container in backgroundContainer starts, terminal freed
3docker run -d nginxReturn container IDOutputs container ID string
4docker psList running containersShows nginx container running with container ID
5EndNo more commandsUser terminal ready for new commands
💡 Container runs in background; terminal is free for new commands
Status Tracker
VariableStartAfter Step 2After Step 3Final
container_modenonedetacheddetacheddetached
container_idnonegenerated_idgenerated_idgenerated_id
terminal_statebusyfreefreefree
Key Moments - 3 Insights
Why does the terminal become free immediately after running 'docker run -d nginx'?
Because the -d flag tells Docker to run the container in detached mode, so it runs in the background and does not block the terminal. See execution_table step 2.
What does Docker output after starting a container in detached mode?
Docker outputs the container ID string so you can identify or manage the container later. See execution_table step 3.
How can you verify that the container is running after starting it detached?
By running 'docker ps' which lists all running containers, showing the nginx container with its ID. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the terminal state immediately after step 2?
Abusy
Bfree
Cunknown
Dwaiting
💡 Hint
Check the 'terminal_state' variable in variable_tracker after Step 2
At which step does Docker output the container ID?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output/Result' column in execution_table for container ID output
If you remove the -d flag, what will happen to the terminal after running 'docker run nginx'?
ADocker will output container ID and exit
BTerminal will be free immediately
CTerminal will stay busy until container stops
DContainer will not start
💡 Hint
Refer to concept_flow where absence of -d flag leads to foreground running
Concept Snapshot
docker run -d <image>
- Runs container in detached mode (background)
- Terminal is free immediately
- Outputs container ID
- Use 'docker ps' to see running containers
Full Transcript
When you run 'docker run -d nginx', Docker checks for the -d flag. Because it is present, Docker starts the nginx container in detached mode, meaning it runs in the background. The terminal becomes free immediately, allowing you to run other commands. Docker outputs the container ID so you can identify the container later. You can verify the container is running by using 'docker ps', which lists all running containers including the nginx one. Without the -d flag, the container runs in the foreground and the terminal stays busy until the container stops.