docker run
Packages and runtimes
Usage
docker run [-d] [-p <host>:<container>] <image> [command]Creates and starts a new container from an image every time, so running it twice gives you two containers and restarting an existing one is docker start; without -p nothing is reachable from the host, and without -v whatever it writes dies with the container.
Common flags
| Flag | Meaning |
|---|---|
| -d | run in the background and print the id |
| -p 8080:80 | publish a container port on the host |
| -it | interactive with a terminal, for a shell |
| --rm | delete the container when it exits |
| -v <host>:<path> | mount a host folder or named volume |
| -e KEY=value | set an environment variable |
| --name <name> | give it a name instead of a random one |
| --network <net> | attach it to a network |
Examples
docker run --rm -it ubuntu basha throwaway shell in a container
docker run -d -p 8080:80 --name web nginxnginx in the background, reachable on localhost:8080
docker run --rm -v "$PWD":/app -w /app node:22 npm testruns your tests in a container without installing node
The question is always what goes where: inside this project, or on the whole machine.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does docker run do?
Creates and starts a new container from an image every time, so running it twice gives you two containers and restarting an existing one is docker start; without -p nothing is reachable from the host, and without -v whatever it writes dies with the container.
Q. How do I type it?
docker run [-d] [-p <host>:<container>] <image> [command] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
8 are listed here; the full set is in man docker. This command sits under Packages and runtimes.
Related commands
man page: man docker-run