0
0
Dockerdevops~5 mins

Running containers in detached mode in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
By default docker run attaches your terminal to the container — when you close the terminal the container stops. Detached mode runs the container in the background so it keeps running independently of your terminal session.
When you start a web server or database container that should keep running after you close your terminal.
When you deploy multiple containers in a script and don't want to wait for each one interactively.
When you run a container on a remote server over SSH and want it to keep running after disconnecting.
When you want to start a container and immediately check its logs or status without being attached.
When you run containers as part of a CI/CD pipeline that needs to continue after launch.
Commands
Starts an nginx container in detached mode. The terminal immediately returns the container ID and the container runs in the background.
Terminal
docker run -d nginx
Expected OutputExpected
d4e5f6a7b8c9012def1234567890abcdef1234567890abcdef1234567890abcd
-d - Detaches the container from the terminal — runs in background
Lists all running containers to confirm the detached nginx container is active.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4e5f6a7b8c9 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 80/tcp relaxed_jones
Fetches the logs of the detached container to check what it is doing without being attached to it.
Terminal
docker logs relaxed_jones
Expected OutputExpected
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Stops the detached container gracefully when you no longer need it running.
Terminal
docker stop relaxed_jones
Expected OutputExpected
relaxed_jones
Key Concept

If you remember nothing else from this pattern, remember: -d detaches the container from your terminal so it keeps running in the background — use 'docker logs' to see what it is doing.

Common Mistakes
Running a container without -d and closing the terminal expecting it to keep running
The container stops when the terminal closes because it is attached to your shell session.
Always add -d when you want the container to run independently of your terminal.
Not knowing how to check a detached container's output
You lose visibility into what the container is doing since it has no attached terminal.
Use 'docker logs container_name' to see output, or 'docker logs -f' to follow live logs.
Confusing -d with --rm
--rm removes the container after it exits; -d just detaches it. Using both removes it on stop.
Use -d alone for persistent containers. Add --rm only for temporary one-off containers.
Summary
Use -d to run a container in the background, detached from your terminal session.
Use 'docker ps' to confirm the detached container is running and get its name or ID.
Use 'docker logs container_name' to view output from a detached container.