Docker Engine 29 reference · Containers

docker run Command (Docker CLI)

Create and start a container from an image, with options for ports, environment, volumes, names and cleanup.

Docker Engine 29

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
--rm Remove the container when it exits.
-d Run in the background and print the container ID.
--name NAME Give the container a name.
-e KEY=VALUE Set an environment variable.
-p HOST:CONTAINER Publish a container port on the host.
-v SOURCE:TARGET Mount a volume or host path.

Examples

Run a one-off command
docker run --rm alpine:3.24 echo hello from a container
docker run --rm -e NAME=Ana alpine:3.24 sh -c 'echo "hi $NAME"'

Output

hello from a container
hi Ana

Run this on your computer — see “Get Started” for setup.

Run in the background
docker run -d --name rc-ref-run alpine:3.24 sleep 300 > /dev/null
docker ps --filter name=rc-ref-run --format '{{.Names}}: {{.State}}'
docker rm -f rc-ref-run > /dev/null

Output

rc-ref-run: running

Run this on your computer — see “Get Started” for setup.

The container lives as long as its main process. Arguments after the image replace the image's CMD. Use --rm for one-off commands so stopped containers don't pile up.

Was this page helpful?