DK

Containers

Docker · 13 entries

Run a Container

syntax
docker run [options] <image> [command]
example
docker run -d --name web-api -p 3000:3000 myapp:1.0
docker run -it ubuntu:22.04 bash
output
e4f5a6b7c8d9...

Note docker run = docker create + docker start. The long hex string printed is the container ID. Use --name so you can refer to it by a human-readable name instead.

Start a Stopped Container

syntax
docker start <container>
example
docker start web-api
docker start -ai my-cli-tool
output
web-api

Note Unlike docker run, this does not create a new container — it restarts an existing one that was previously stopped. Use -ai to reattach stdin and see output.

Stop a Running Container

syntax
docker stop [options] <container> [<container>...]
example
docker stop web-api
docker stop -t 5 web-api
output
web-api

Note Sends SIGTERM first and waits 10 seconds (default) before sending SIGKILL. Use -t to change the grace period. If your app does not handle SIGTERM, it will always take the full timeout before dying.

Restart a Container

syntax
docker restart <container>
example
docker restart web-api
docker restart -t 3 web-api
output
web-api

Note This performs a stop followed by a start. The container keeps its filesystem and configuration — only the process is restarted.

Remove a Container

syntax
docker rm <container> [<container>...]
docker rm -f <container>
example
docker rm web-api
docker rm -f $(docker ps -aq)
output
web-api

Note You cannot remove a running container without -f. The second example force-removes ALL containers — obviously destructive. Data in unnamed volumes is lost unless you use -v to also remove them intentionally.

List Containers

syntax
docker ps [options]
example
docker ps
docker ps -a
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
output
CONTAINER ID   IMAGE       STATUS          PORTS                    NAMES
e4f5a6b7c8d9   myapp:1.0   Up 2 minutes    0.0.0.0:3000->3000/tcp   web-api

Note Without -a you only see running containers. Stopped containers still exist and consume disk space until you docker rm them.

View Container Logs

syntax
docker logs [options] <container>
example
docker logs web-api
docker logs -f --tail 50 web-api
docker logs --since 10m web-api
output
Server listening on port 3000
GET /api/users 200 12ms

Note Use -f to follow in real time (like tail -f). Combine --tail and --since to avoid dumping thousands of lines. Logs persist until the container is removed.

Execute a Command in a Running Container

syntax
docker exec [options] <container> <command>
example
docker exec -it web-api sh
docker exec web-api cat /app/config.json
docker exec -u root web-api apt-get update
output
/ #

Note The container must be running. Use -it for an interactive shell. The -u flag lets you override the user (e.g., run as root even if the image uses a non-root user).

Attach to a Running Container

syntax
docker attach <container>
example
docker attach web-api

Note This connects your terminal to the container's main process (PID 1). Pressing Ctrl+C sends SIGINT to that process and may stop the container. Use Ctrl+P, Ctrl+Q to detach without stopping. Prefer docker exec for most debugging scenarios.

Rename a Container

syntax
docker rename <old_name> <new_name>
example
docker rename web-api web-api-legacy

Note Works on running or stopped containers. Other containers referencing the old name via DNS in a user-defined network will no longer resolve it.

Live Resource Usage

syntax
docker stats [container...]
example
docker stats
docker stats web-api db-postgres --no-stream
output
CONTAINER   CPU %   MEM USAGE / LIMIT     NET I/O         BLOCK I/O
web-api     0.50%   85.2MiB / 512MiB      1.2kB / 648B    0B / 4.1kB

Note Without arguments, it shows all running containers. Add --no-stream to get a single snapshot instead of a live-updating display.

Wait for Container to Stop

syntax
docker wait <container>
example
docker wait batch-job && echo 'Job finished'
output
0

Note Blocks until the container stops and then prints the exit code. Exit code 0 means success. Useful in scripts that need to wait for a task container to finish before proceeding.

Show Port Mappings

syntax
docker port <container>
example
docker port web-api
output
3000/tcp -> 0.0.0.0:3000
8443/tcp -> 0.0.0.0:8443

Note Only shows ports explicitly published with -p at run time. Ports declared with EXPOSE in the Dockerfile are not listed here unless they were also published.