Skip to main content

How to Run a Docker Container in the Background

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

docker run on its own keeps the terminal busy until the container stops. With -d, Docker starts the container in the background, prints its ID and gives the terminal back, so a server can keep running while you use the terminal for something else. This guide starts the nginx web server that way, names it, and checks that it's running, with real output from Docker Desktop 4.100.1.

Syntax​

Syntax
docker run -d --name NAME IMAGE
PartWhat it means
-d, --detachStart the container in the background and print its ID
--name NAMEA name to use in later commands instead of the ID
IMAGEThe image to start, such as nginx:alpine

The docker run reference: "-d, --detach Run container in background and print container ID" and "--name Assign a name to the container". The same commands work in PowerShell.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running. Docker's licence terms make it free for personal use, education, non-commercial open source and small businesses.

Step 1: Download the Image​

nginx:alpine is the nginx web server on Alpine Linux, a small image:

Terminal
docker pull nginx:alpine
Output
alpine: Pulling from library/nginx
b90627b8f429: Already exists
6d14a3e596f2: Pull complete
...
Digest: sha256:7fc0fb4af7bf006aeb31aea88069976060f8f0a0a86b667126742cc17f4e31ae
Status: Downloaded newer image for nginx:alpine
docker.io/library/nginx:alpine

docker run would download it by itself too; pulling first keeps the download out of the next step.

Step 2: Start the Container in the Background​

Terminal
docker run -d --name web nginx:alpine
Output
540be37b12b6f98901076227c8500b9792513020d194bd33bff10ff941a25470

docker run -d's output in a terminal: the new container's 64-character ID.

That's the container's ID; the terminal is free again. Later commands can use web instead.

Step 3: Check That It's Running​

Terminal
docker ps --filter name=web
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
540be37b12b6 nginx:alpine "/docker-entrypoint.…" 2 seconds ago Up 1 second 80/tcp web

docker ps's output in a terminal: the container web from nginx, STATUS Up 1 second, PORTS 80/tcp.

STATUS Up 1 second: it's running. docker ps lists running containers, and --filter name=web keeps the list to this one. PORTS 80/tcp is the port nginx listens on inside the container; nothing outside can reach it yet: How to Publish a Docker Container's Port Only on Your Computer.

To run a command inside it, such as nginx's version:

Terminal
docker exec web nginx -v
Output
nginx version: nginx/1.31.10

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting -d for a serverThe terminal stays busy with the container's outputAdd -d
Starting a second container with the same --nameConflict. The container name "/web" is already in use by container "..."Remove the first, or use another name

Troubleshooting​

SymptomLikely causeFix
Cannot connect to the Docker daemonDocker Desktop isn't runningFixed: Cannot connect to the Docker daemon
The container isn't in docker psIt stopped alreadydocker ps -a shows stopped ones, and docker logs NAME why

FAQ​

How do I see its output?​

docker logs web: How to See the Logs of a Docker Container.

How do I stop it?​

docker stop web, then docker rm web to delete it: How to Stop and Remove a Docker Container.

Conclusion​

docker run -d --name web nginx:alpine started nginx in the background, printed the container's ID and gave the terminal back, and docker ps --filter name=web showed it Up.

Additional Resources​