Running Containers
A comprehensive developer cheatsheet covering container lifecycle management, featuring essential CLI commands and operational workflows for running, listing, starting, stopping, pausing, killing, and renaming Docker containers.
docker run — Create and Run a Container
Creates and starts a new container from an image.
Basic Usage
$ docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2026/01/15 10:30:15 [notice] 1#1: using the "epoll" event method
2026/01/15 10:30:15 [notice] 1#1: nginx/1.25.3Common Flags
| Flag | Purpose |
|---|---|
-d, --detach | Run in background |
-it | Interactive terminal |
--name | Assign a name to the container |
-p, --publish | Publish a port |
-v, --volume | Mount a volume |
--rm | Auto-remove container on exit |
-e, --env | Set environment variable |
--restart | Restart policy (always, on-failure) |
-w, --workdir | Working directory |
-u, --user | Username or UID |
--entrypoint | Override entrypoint |
--network | Connect to a network |
Examples
Run in background (detached):
$ docker run -d nginx
abc123def456...Run with a name:
$ docker run -d --name web-server nginxRun with port mapping:
$ docker run -d -p 8080:80 --name web-server nginx
# Access at http://localhost:8080Run interactively (shell):
$ docker run -it ubuntu /bin/bash
root@abc123:/#Run with environment variable:
$ docker run -d -e ENV=production --name app myapp:latestRun with volume mount:
$ docker run -d -v /host/data:/app/data --name app myapp:latestRun with multiple ports:
$ docker run -d -p 8080:80 -p 8443:443 --name web-server nginxRun and auto-remove on exit:
$ docker run --rm -it ubuntu /bin/bash
# Container is removed when you exitRun with restart policy:
$ docker run -d --restart=always --name web-server nginxRun with custom command:
$ docker run ubuntu echo "Hello World"
Hello WorldRun with working directory:
$ docker run -w /app -it ubuntu /bin/bashRun with user:
$ docker run -u 1000 -it ubuntu /bin/bashRun with memory limit:
$ docker run -d --memory=512m --name app myapp:latestRun with CPU limit:
$ docker run -d --cpus=0.5 --name app myapp:latestdocker ps — List Containers
Lists running containers.
Basic Usage
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp web-server
def456789abc ubuntu "/bin/bash" 10 minutes ago Up 10 minutes app-containerCommon Flags
| Flag | Purpose |
|---|---|
-a, --all | Show all containers (including stopped) |
-q, --quiet | Show only container IDs |
-l, --latest | Show the latest created container |
-n N | Show N last created containers |
--filter | Filter results (status, name, label) |
--format | Format output using Go template |
Examples
Show all containers (including stopped):
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp web-server
def456789abc ubuntu "/bin/bash" 10 minutes ago Exited (0) 2 minutes ago app-containerShow only container IDs:
$ docker ps -q
abc123def456
def456789abcShow latest container:
$ docker ps -lShow last 3 containers:
$ docker ps -n 3Filter by status:
$ docker ps --filter "status=exited"
$ docker ps --filter "status=running"
$ docker ps --filter "status=stopped"Filter by name:
$ docker ps --filter "name=web"Filter by label:
$ docker ps --filter "label=app=nginx"Format output:
$ docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"▶ docker start — Start a Container
Starts one or more stopped containers.
Basic Usage
$ docker start web-server
web-serverCommon Flags
| Flag | Purpose |
|---|---|
-a, --attach | Attach to container output |
-i, --interactive | Attach to container's STDIN |
Examples
Start with attach:
$ docker start -a web-serverStart multiple containers:
$ docker start web-server app-containerdocker stop — Stop a Container
Stops one or more running containers.
Basic Usage
$ docker stop web-server
web-serverCommon Flags
| Flag | Purpose |
|---|---|
-t, --time | Seconds to wait before killing (default 10) |
Examples
Stop with custom timeout:
$ docker stop -t 5 web-serverStop multiple containers:
$ docker stop web-server app-containerdocker restart — Restart a Container
Restarts one or more containers.
Basic Usage
$ docker restart web-server
web-serverCommon Flags
| Flag | Purpose |
|---|---|
-t, --time | Seconds to wait before killing (default 10) |
Examples
Restart with custom timeout:
$ docker restart -t 5 web-serverRestart multiple containers:
$ docker restart web-server app-containerdocker pause — Pause a Container
Pauses all processes in a container.
Basic Usage
$ docker pause web-server
web-server▶ docker unpause — Unpause a Container
Unpauses a paused container.
Basic Usage
$ docker unpause web-server
web-serverdocker wait — Wait for a Container
Blocks until a container stops, then returns its exit code.
Basic Usage
$ docker wait web-server
0Run in background and wait:
$ docker run -d nginx && docker wait web-serverdocker kill — Kill a Container
Sends a SIGKILL to a running container.
Basic Usage
$ docker kill web-server
web-serverCommon Flags
| Flag | Purpose |
|---|---|
-s, --signal | Signal to send (default SIGKILL) |
Examples
Send specific signal:
$ docker kill -s SIGTERM web-serverdocker rename — Rename a Container
Renames a container.
Basic Usage
$ docker rename web-server my-nginxQuick Reference — Most Used Commands
| Task | Command |
|---|---|
| Run container (detached) | docker run -d nginx |
| Run container with name | docker run -d --name web nginx |
| Run container with port | docker run -d -p 8080:80 nginx |
| Run interactive shell | docker run -it ubuntu /bin/bash |
| List running containers | docker ps |
| List all containers | docker ps -a |
| List container IDs | docker ps -q |
| Start a container | docker start web-server |
| Stop a container | docker stop web-server |
| Restart a container | docker restart web-server |
| Pause a container | docker pause web-server |
| Unpause a container | docker unpause web-server |
| Wait for a container | docker wait web-server |
| Kill a container | docker kill web-server |
| Rename a container | docker rename web-server my-nginx |
Pro Tips
-
Always name containers — makes management easier.
-
Use
--rmfor temporary containers — auto-cleanup after exit. -
Check container logs after starting:
docker logs -f web-server. -
Graceful shutdown —
docker stopsends SIGTERM (15),docker killsends SIGKILL (9). -
Default timeout for
stopis 10 seconds. Increase if needed. -
Pause for debugging — pauses container processes without stopping.
-
Wait in scripts —
docker waitreturns exit code. -
docker ps -a -q— get all container IDs for bulk operations.