Your first docker run
What happens at start and how to find a stopped container.
The docker run command does several steps. First it looks for the image locally and, if it is missing, downloads it from a registry (Docker Hub by default). Then it creates a container: a separate writable layer, network settings and a name. Finally it runs the command inside. The same steps are available separately: docker pull, docker create and docker start.
A few flags come up all the time. -d starts the container in the background and immediately returns its ID. -it attaches a terminal so you can work inside interactively. --name sets a readable name instead of a random one. --rm removes the container after it stops. Arguments after the image name replace the default command.
docker run -d --name web nginx:1.27
docker ps
# CONTAINER ID IMAGE STATUS NAMES
# 3f2a... nginx:1.27 Up 5 seconds web
docker run -it --rm alpine:3.20 sh
The first command starts a web server in the background, docker ps shows running containers, and the third opens a shell inside a temporary container.
Stopped containers disappear from docker ps, but not from the disk. You can see them in docker ps -a together with the exit code in the STATUS column: Exited (0) means success, any other number means an error. It is easy to pile up dozens of forgotten containers this way, so --rm is handy for one-off commands, and containers you no longer need are removed with docker rm.
A common mistake is to start a service without -d and close the terminal. The container receives a signal and stops together with the session. The opposite mistake is to start a one-off command with -d and wonder why nothing appears: the output went to the container's logs, and you can read it with docker logs and the container name.
Practice
Steps of docker run
The
nginx:1.27image is not on the computer. What willdocker run nginx:1.27do?- Fail with an error: docker pull is needed first
- Download the image, create a container and start it
- Build an image from the Dockerfile in the current folder
- Create a container but not start it
Start flags
Which statements about
docker runflags are true? Mark all.- -d starts the container in the background
- --rm removes the container after it stops
- -it starts the container in the background without a terminal
- --name sets the container name
Where are the stopped ones
Which command lists all containers, including stopped ones?