This page looks best with JavaScript enabled

Docker: Running a Container with docker run

 ·   ·  ☕ 1 min read

docker run starts a container from an image. A handful of flags cover most day-to-day use. This example serves the current folder with nginx, so you can open it at http://localhost:8080:

1
docker run --rm -it -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx

${PWD} is the current directory in bash and PowerShell. In a Windows command prompt use %cd% instead. The cheat sheet below breaks down each part.


Sam Wronski
WRITTEN BY
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!

docker run flags

Serves the current folder at http://localhost:8080 using the nginx image.

docker run --rm -it -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx
docker run
start a new container from an image
--rm
delete the container automatically when it exits, so stopped containers don't pile up
-it
-i keeps input open and -t gives it a terminal, so you see the logs and can stop it with Ctrl+C
-p 8080:80
publish a port as host:container, so localhost:8080 reaches port 80 inside the container
-v ${PWD}:/usr/share/nginx/html
mount a folder as host-path:container-path; here the current folder replaces nginx's web root
nginx
the image to run

Also useful

-d
Run in the background (detached) instead of attaching your terminal.
--name my-site
Give the container a name so you can refer to it later.
docker ps
List running containers.
docker stop my-site
Stop a container by name or id.

docker run reference