containers

docker run

Creates and starts a new container from a specified image in a single step. This is the primary command for launching workloads and supports extensive configuration for networking, storage, and process control.

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

Common flags

Flag Purpose
-d, --detach Run container in background and print container ID
-p, --publish Publish container port to host (HOST:CONTAINER)
-v, --volume Bind mount a volume or host path into the container
--rm Automatically remove the container when it exits
--name Assign a human-readable name to the container
-e, --env Set an environment variable inside the container
--network Connect container to a specified network
-it Interactive TTY session (combines --interactive and --tty)

Examples

docker run -d -p 8080:80 --name web nginx:alpine

Detached Nginx published on host port 8080

docker run --rm -it ubuntu:24.04 bash

Ephemeral interactive shell that auto-cleans on exit

docker run -d -v mydata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:16

Persistent Postgres with a named volume

docker run --network host --name monitor prom/node-exporter

Share host network namespace (Linux only)

Gotcha

-p HOST:CONTAINER order matters; reversing publishes the wrong port. --network host is not supported on Docker Desktop for Mac/Windows the same way as on Linux.

Related commands

← All Docker commands · Docker vs Kubernetes · Docker vs VMs