docker run

Create and start a new container from an image. The most common Docker command for launching containers.

Syntax

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Examples

Example 1
docker run nginx

Run nginx in foreground (blocks terminal).

Example 2
docker run -d --name my-app -p 8080:80 nginx

Run in detached mode with custom name and port mapping.

Example 3
docker run -it --rm ubuntu bash

Interactive shell, remove container when it exits.

Example 4
docker run -e NODE_ENV=production -v $(pwd):/app node:20

Set env var and bind-mount current directory.

Common flags

FlagDescription
-d, --detachRun in background
-itInteractive TTY (combine -i -t)
--nameAssign a name to the container
-p, --publishMap host:container ports
-v, --volumeBind mount a volume
-e, --envSet environment variable
--rmRemove container when it exits

Tips & best practices

Related commands

View full Docker cheatsheet →