Sub topic 2
Starting a container with docker run
This chapter assumes you have Docker Engine or Docker Desktop installed, can use a terminal, and know what a process and an environment variable are. You should also know that an image is a read-only template and a container is a running instance of it.
What docker run does
docker run is a shortcut for several steps. It pulls the image if it is not stored locally, creates a container from it, and starts the main process. The general form is:
docker run [OPTIONS] IMAGE [COMMAND] [ARGS...]Options go before the image name. Anything after the image name is passed to the container as the command to run. If you put an option after the image, Docker treats it as part of that command.
Your first container
The smallest test is the hello-world image. The --rm flag removes the container when it exits.
docker run --rm hello-worldDocker prints a short message and exits. The container ran one process, that process finished, and the container stopped. A container lives only as long as its main process.
What happens behind the command
Running a long-lived service
Most real containers run a server. This command starts nginx in the background, gives it the name web, and maps host port 8080 to container port 80:
docker run -d --name web -p 8080:80 nginx:1.27The options used here:
| Option | Meaning |
|---|---|
| `-d` | Run in the background (detached) and print the container ID |
| `--name web` | Assign a name you can use in later commands |
| `-p 8080:80` | Publish container port 80 on host port 8080 |
| `nginx:1.27` | The image, with a tag that selects a version |
Open http://localhost:8080 and you should see the nginx welcome page.
Choosing the image tag
An image reference has the form name:tag. If you omit the tag, Docker uses latest. That tag can point to a different image next month, so pin a specific version such as nginx:1.27 when you want repeatable results.
Overriding the command
The text after the image replaces the image's default command. This runs a one-off command in a fresh container:
docker run --rm alpine:3.20 echo "hello from alpine"The container starts, runs echo, prints the text, and is removed.
Common errors
- Name already in use. Names must be unique, even among stopped containers. Running the nginx command twice fails with a conflict. Remove the old container with
docker rm web, or choose another name. - Port is already allocated. Another process or container is using host port 8080. Pick a different host port, such as
-p 8081:80. - Unable to find image. The name or tag is misspelled, or the image is in a private registry you have not logged in to.
- Container exits immediately. The main process ended. This is normal for
hello-worldbut a problem for a server. Check the output withdocker logs NAME.
Exercise
You run docker run nginx:1.27 -d -p 8080:80. The command fails or behaves unexpectedly. Why, and how do you fix it?
Show answer
Options placed after the image name are passed to the container as its command, not read by Docker. Move them before the image: docker run -d -p 8080:80 nginx:1.27.
Exercise
A container started from hello-world is not listed by docker ps. Why?
Show answer
Its main process finished and the container exited. docker ps shows only running containers. Use docker ps -a to include stopped ones. With --rm it is removed, so it would not appear even there.