Hands-on task
In the background, with a name
Run the nginx web server from the nginx:1.27-alpine image so that it runs in the background and your terminal stays free, and name the container web.
- docker run -d
- --name
Hands-on task
Run the nginx web server from the nginx:1.27-alpine image so that it runs in the background and your terminal stays free, and name the container web.
docker run has two modes. By default (foreground) the container is attached to your terminal: its output goes to the screen, and Ctrl+C stops it. Services — web servers, databases — need background mode, the -d flag (detached): Docker starts the container, prints its ID and gives you the prompt back right away.
Without --name, Docker makes up a name itself — something like funny_turing. With --name you choose the name, and it's handy for referring to the container in other commands instead of the long ID. The name must be unique: Docker won't create a second container with the same name, even if the first one is stopped.
docker run -d --name timer busybox:1.36 sleep 600
docker ps # timer with the status Up
docker ps shows running containers: name, image, status (Up … means running). It doesn't show stopped ones — add -a for the full list, and the exited ones will have the status Exited (code).
Flags for docker run go between run and the image name, and everything after the image is the container's command.
-d doesn't extend a container's life: it lives as long as its main process runs. For example, docker run -d alpine:3.21 exits immediately — the sh shell has nothing to do without input.