Running Docker Containers In Detached Mode
Run containers in the background using detached mode and manage them with common Docker commands.
By default, many containers run in the foreground.
That means the container logs stay attached to your terminal. If the application keeps running, your terminal stays busy until you stop the container or open another terminal.
Detached mode solves this by running the container in the background.
Foreground mode
Run Nginx normally:
docker run nginxThe container starts, and the terminal stays attached to it. You may see logs directly in the terminal.
This is useful for learning and debugging, but it is not always convenient.
Detached mode
Use -d to run a container in detached mode:
docker run -d nginxDocker starts the container in the background and prints the container ID.
Now your terminal is free for more commands.
Check the running container
Use:
docker psYou should see the container running.
The output shows details such as:
- Container ID
- Image name
- Command
- Created time
- Status
- Ports
- Container name
View logs from detached containers
Detached does not mean invisible. You can still view logs:
docker logs container-idTo continuously follow logs:
docker logs -f container-idPress Ctrl + C to stop following logs. This does not stop the container.
Stop a detached container
To stop the container:
docker stop container-idThen confirm:
docker ps
docker ps -aThe first command shows running containers. The second shows stopped containers too.
Naming a container
Container IDs are long, so names are easier during practice.
docker run -d --name my-nginx nginxNow you can use the name:
docker logs my-nginx
docker stop my-nginx
docker rm my-nginxWhen to use detached mode
Use detached mode when:
- Running web servers
- Running databases for local development
- Running services that should continue in the background
- Testing port mapping
- Practicing real deployment style
Foreground mode is good for seeing what happens immediately. Detached mode is closer to how long-running services are usually run.
Written By: Shiva Srivastava
How is this guide?
Last updated on
