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
docker ps
Lists containers along with their status, ports, and identifiers. By default it shows only running containers unless -a is passed.
docker exec
Runs a new command inside an already running container. Commonly used for shells, debugging, or one-off maintenance tasks.
docker logs
Fetches the stdout/stderr logs of a container. Works with the json-file and local logging drivers; other drivers require querying the destination directly.
docker stop
Gracefully stops one or more running containers by sending SIGTERM and then SIGKILL after a timeout. Use docker kill for an immediate SIGKILL.
docker start
Starts one or more previously created or stopped containers using their existing configuration. Unlike docker run it does not create a new container.
← All Docker commands · Docker vs Kubernetes · Docker vs VMs