New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsDockerDocker
FreeSheet 03 of 11

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

bash
$ 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.3

Common Flags

FlagPurpose
-d, --detachRun in background
-itInteractive terminal
--nameAssign a name to the container
-p, --publishPublish a port
-v, --volumeMount a volume
--rmAuto-remove container on exit
-e, --envSet environment variable
--restartRestart policy (always, on-failure)
-w, --workdirWorking directory
-u, --userUsername or UID
--entrypointOverride entrypoint
--networkConnect to a network

Examples

Run in background (detached):

bash
$ docker run -d nginx
abc123def456...

Run with a name:

bash
$ docker run -d --name web-server nginx

Run with port mapping:

bash
$ docker run -d -p 8080:80 --name web-server nginx
# Access at http://localhost:8080

Run interactively (shell):

bash
$ docker run -it ubuntu /bin/bash
root@abc123:/#

Run with environment variable:

bash
$ docker run -d -e ENV=production --name app myapp:latest

Run with volume mount:

bash
$ docker run -d -v /host/data:/app/data --name app myapp:latest

Run with multiple ports:

bash
$ docker run -d -p 8080:80 -p 8443:443 --name web-server nginx

Run and auto-remove on exit:

bash
$ docker run --rm -it ubuntu /bin/bash
# Container is removed when you exit

Run with restart policy:

bash
$ docker run -d --restart=always --name web-server nginx

Run with custom command:

bash
$ docker run ubuntu echo "Hello World"
Hello World

Run with working directory:

bash
$ docker run -w /app -it ubuntu /bin/bash

Run with user:

bash
$ docker run -u 1000 -it ubuntu /bin/bash

Run with memory limit:

bash
$ docker run -d --memory=512m --name app myapp:latest

Run with CPU limit:

bash
$ docker run -d --cpus=0.5 --name app myapp:latest

docker ps — List Containers

Lists running containers.

Basic Usage

bash
$ 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-container

Common Flags

FlagPurpose
-a, --allShow all containers (including stopped)
-q, --quietShow only container IDs
-l, --latestShow the latest created container
-n NShow N last created containers
--filterFilter results (status, name, label)
--formatFormat output using Go template

Examples

Show all containers (including stopped):

bash
$ 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-container

Show only container IDs:

bash
$ docker ps -q
abc123def456
def456789abc

Show latest container:

bash
$ docker ps -l

Show last 3 containers:

bash
$ docker ps -n 3

Filter by status:

bash
$ docker ps --filter "status=exited"
$ docker ps --filter "status=running"
$ docker ps --filter "status=stopped"

Filter by name:

bash
$ docker ps --filter "name=web"

Filter by label:

bash
$ docker ps --filter "label=app=nginx"

Format output:

bash
$ docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

▶ docker start — Start a Container

Starts one or more stopped containers.

Basic Usage

bash
$ docker start web-server
web-server

Common Flags

FlagPurpose
-a, --attachAttach to container output
-i, --interactiveAttach to container's STDIN

Examples

Start with attach:

bash
$ docker start -a web-server

Start multiple containers:

bash
$ docker start web-server app-container

docker stop — Stop a Container

Stops one or more running containers.

Basic Usage

bash
$ docker stop web-server
web-server

Common Flags

FlagPurpose
-t, --timeSeconds to wait before killing (default 10)

Examples

Stop with custom timeout:

bash
$ docker stop -t 5 web-server

Stop multiple containers:

bash
$ docker stop web-server app-container

docker restart — Restart a Container

Restarts one or more containers.

Basic Usage

bash
$ docker restart web-server
web-server

Common Flags

FlagPurpose
-t, --timeSeconds to wait before killing (default 10)

Examples

Restart with custom timeout:

bash
$ docker restart -t 5 web-server

Restart multiple containers:

bash
$ docker restart web-server app-container

docker pause — Pause a Container

Pauses all processes in a container.

Basic Usage

bash
$ docker pause web-server
web-server

▶ docker unpause — Unpause a Container

Unpauses a paused container.

Basic Usage

bash
$ docker unpause web-server
web-server

docker wait — Wait for a Container

Blocks until a container stops, then returns its exit code.

Basic Usage

bash
$ docker wait web-server
0

Run in background and wait:

bash
$ docker run -d nginx && docker wait web-server

docker kill — Kill a Container

Sends a SIGKILL to a running container.

Basic Usage

bash
$ docker kill web-server
web-server

Common Flags

FlagPurpose
-s, --signalSignal to send (default SIGKILL)

Examples

Send specific signal:

bash
$ docker kill -s SIGTERM web-server

docker rename — Rename a Container

Renames a container.

Basic Usage

bash
$ docker rename web-server my-nginx

Quick Reference — Most Used Commands

TaskCommand
Run container (detached)docker run -d nginx
Run container with namedocker run -d --name web nginx
Run container with portdocker run -d -p 8080:80 nginx
Run interactive shelldocker run -it ubuntu /bin/bash
List running containersdocker ps
List all containersdocker ps -a
List container IDsdocker ps -q
Start a containerdocker start web-server
Stop a containerdocker stop web-server
Restart a containerdocker restart web-server
Pause a containerdocker pause web-server
Unpause a containerdocker unpause web-server
Wait for a containerdocker wait web-server
Kill a containerdocker kill web-server
Rename a containerdocker rename web-server my-nginx

Pro Tips

  1. Always name containers — makes management easier.

  2. Use --rm for temporary containers — auto-cleanup after exit.

  3. Check container logs after starting: docker logs -f web-server.

  4. Graceful shutdown — docker stop sends SIGTERM (15), docker kill sends SIGKILL (9).

  5. Default timeout for stop is 10 seconds. Increase if needed.

  6. Pause for debugging — pauses container processes without stopping.

  7. Wait in scripts — docker wait returns exit code.

  8. docker ps -a -q — get all container IDs for bulk operations.


Next in DockerContainer Management