Home·Terminal commands

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

FlagMeaning
-drun in the background and print the id
-p 8080:80publish a container port on the host
-itinteractive with a terminal, for a shell
--rmdelete the container when it exits
-v <host>:<path>mount a host folder or named volume
-e KEY=valueset 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 bash

a throwaway shell in a container

docker run -d -p 8080:80 --name web nginx

nginx in the background, reachable on localhost:8080

docker run --rm -v "$PWD":/app -w /app node:22 npm test

runs 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