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 herenothing runs on your computer
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
| Option | What it does |
|---|---|
-d | run in the background (detached) |
--name | give it a name (else Docker picks one) |
-p | publish a port: HOST:CONTAINER |
-e | set an environment variable |
-v | mount a volume: VOLUME:PATH |
--rm | delete the container when it stops |
-it | interactive, with a terminal |
Good to know
- Read
-p 8080:80as "my 8080 goes to the container's 80". - A name can only be used once:
docker rmthe old container before reusing it.