Docker Container Operations
This chapter covers the complete Docker container workflow, including creating, starting, stopping, deleting, monitoring, and managing containers.
Container Lifecycle
docker create docker start docker pause
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Created │───►│ Running │───►│ Paused │
└──────────────┘ └──────┬───────┘ └──────┬───────┘
│ │
│ docker stop │ docker unpause
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Stopped │ │ Running │
└──────┬───────┘ └──────────────┘
│ docker rm
▼
┌──────────────┐
│ Removed │
└──────────────┘Creating and Running Containers
docker run Basics
bash
# Simplest form
docker run nginx
# Run in background (detached mode)
docker run -d nginx
# Name the container
docker run -d --name my-nginx nginx
# Port mapping (host:container)
docker run -d -p 8080:80 --name my-nginx nginx
# Interactive mode
docker run -it ubuntu:22.04 /bin/bashCommon run Parameters
| Parameter | Description | Example |
|---|---|---|
-d | Run in background | docker run -d nginx |
-p | Port mapping | -p 8080:80 |
-v | Volume mount | -v /host/path:/container/path |
-e | Environment variable | -e MYSQL_ROOT_PASSWORD=123456 |
--name | Container name | --name my-app |
--rm | Auto-remove on exit | docker run --rm nginx |
--restart | Restart policy | --restart=always |
-m | Memory limit | -m 512m |
--cpus | CPU limit | --cpus 2 |
--network | Specify network | --network my-net |
Container Management
Listing Containers
bash
docker ps # Running containers
docker ps -a # All containers
docker ps -q # Only IDs
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"
docker ps -f "status=running"Start, Stop, and Restart
bash
docker start my-nginx # Start stopped container
docker stop my-nginx # Graceful stop (SIGTERM, then SIGKILL after 10s)
docker kill my-nginx # Force stop immediately
docker restart my-nginx # Restart
docker pause my-nginx # Freeze processes
docker unpause my-nginx # ResumeRemoving Containers
bash
docker rm my-nginx # Remove stopped container
docker rm -f my-nginx # Force remove running container
docker container prune # Remove all stopped containers
docker rm -f $(docker ps -aq) # Remove all containersContainer Interaction
Entering a Running Container
bash
docker exec -it my-nginx /bin/bash # Using bash
docker exec -it my-nginx /bin/sh # Using sh
docker exec -it -u root my-nginx bash # As root
docker exec my-nginx cat /etc/nginx/nginx.conf # Single commandViewing Logs
bash
docker logs my-nginx # All logs
docker logs -f my-nginx # Follow logs
docker logs --tail 100 my-nginx # Last 100 lines
docker logs -t my-nginx # With timestampsContainer Details
bash
docker inspect my-nginx
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-nginx
docker port my-nginxResource Monitoring
bash
docker stats # Real-time resource usage
docker stats my-nginx # Specific container
docker top my-nginx # Container processesFile Transfer
bash
docker cp ./index.html my-nginx:/usr/share/nginx/html/ # Host → Container
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf # Container → HostRestart Policies
| Policy | Description |
|---|---|
no | Default, no auto-restart |
on-failure[:max] | Restart on non-zero exit, optional max retries |
always | Always restart |
unless-stopped | Always restart unless manually stopped |
bash
docker run -d --restart=always --name my-nginx nginx
docker update --restart=unless-stopped my-nginxUseful Tips
Batch Operations
bash
docker stop $(docker ps -q) # Stop all running
docker container prune -f # Remove all stopped
docker rm -f $(docker ps -aq) # Remove allResource Limits
bash
docker run -d -m 256m --cpus 0.5 --name limited-nginx nginx
docker update -m 512m --cpus 1 limited-nginxChapter Summary
Mastering container operations is fundamental to using Docker. The key commands are docker run, docker exec, docker logs, and docker stats.