docker run is pull, create and start in one command.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
│ │ └─ optional: replaces the image's default command
│ └──────── image reference, pulled if not local
└────────────────── flags such as --name, --rmWhat happens
docker run hello-world
1. image local? no -> pull hello-world:latest from docker.io
2. create a container: new ID, writable layer, generated name
3. start the image's default command (/hello) as PID 1
4. attach your terminal to its output
5. exit when /hello exits, the container exits; docker run returnsdocker run hello-worldUnable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
149e3f68f175: Pull complete
Digest: sha256:2b4e985f0414f3c1d3c53246fb0a1fdd03de66b6aae422acadcddf0d6207ee0d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...The second run skips the pull and starts at once.
Pass a command
Anything after the image name is the command to run instead of the image's default:
docker run alpine:3.20 echo "Hello from pier"Hello from pierThe command must exist in the image. Alpine has echo, cat, ls, uname; it has no bash or curl.
A program that does not exit
A server runs until something stops it. docker run stays attached to it in the foreground:
docker run --name web nginx:1.27/docker-entrypoint.sh: Configuration complete; ready for start up
2026/10/11 20:44:10 [notice] 1#1: using the "epoll" event method
2026/10/11 20:44:10 [notice] 1#1: nginx/1.27.2
2026/10/11 20:44:10 [notice] 1#1: start worker processesThe prompt does not come back. Press Ctrl+C: Docker forwards the signal to nginx, nginx shuts down, and the container exits.
^C
2026/10/11 20:44:13 [notice] 1#1: signal 2 (SIGINT) received, exiting1#1 is nginx's process ID: PID 1 inside the container. Chapter 2 runs servers in the background with -d.
Useful flags for now
| Flag | Effect |
|---|---|
--name web | Name the container web instead of a generated name like sharp_torvalds. Names are unique per host. |
--rm | Delete the container when it exits. The image stays. |
Reusing a name fails until the old container is removed:
docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "99fddfcd7640...". You have to remove (or rename) that container to be able to reuse that name.Running versus all containers
docker ps
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99fddfcd7640 nginx:1.27 "/docker-entrypoint.…" 6 seconds ago Exited (0) 3 seconds ago web
e1dd94dc29ef alpine:3.20 "echo Hello from pier" 13 seconds ago Exited (0) 13 seconds ago sharp_torvalds| Column | Meaning |
|---|---|
COMMAND | The main process, truncated |
STATUS | Up 5 seconds while running, Exited (N) after, where N is the exit code |
NAMES | Your --name, or a generated one |