docker run

Create and start a container from an image, with ports, settings and volumes, explained flag by flag.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • docker run -d --name web nginx
  • docker run -d -p 8080:80 --name site nginx
  • curl localhost:8080
  • docker run --rm alpine echo "hello from a container"
  • docker run -d --name db -e POSTGRES_PASSWORD=secret -v db-data:/var/lib/postgresql/data postgres

How it works

docker run creates a new container from an image and starts it. If the image isn't on your machine yet, Docker downloads it first.

The options set the container up for good: its name, which ports reach it, its settings and where its data lives. To change them later you recreate the container.

Syntax

docker run [OPTIONS] IMAGE [COMMAND]

Options

OptionWhat it does
-drun in the background (detached)
--namegive it a name (else Docker picks one)
-ppublish a port: HOST:CONTAINER
-eset an environment variable
-vmount a volume: VOLUME:PATH
--rmdelete the container when it stops
-itinteractive, with a terminal

Good to know

  • Read -p 8080:80 as "my 8080 goes to the container's 80".
  • A name can only be used once: docker rm the old container before reusing it.