Managing Docker Containers
Run, inspect, stop, restart, and remove containers. Understand container lifecycle and port mapping.
Container Lifecycle
docker run ──► RUNNING ──► docker stop ──► STOPPED
│ │
│ docker start
│ │
docker pause ▼
│ RUNNING
▼
PAUSED
Running Containers
# Run a container (pull image if not local)
docker run nginx
# Run in detached mode (background)
docker run -d nginx
# Run with a name
docker run -d --name my-nginx nginx
# Run and remove container on exit (great for one-off tasks)
docker run --rm alpine echo "hello"
# Run interactively with a shell
docker run -it ubuntu bash
# Map host port 8080 to container port 80
docker run -d -p 8080:80 nginx
# Pass environment variables
docker run -d -e NODE_ENV=production -e PORT=3000 myapp:1.0
# Limit CPU and memory
docker run -d --cpus="0.5" --memory="256m" myapp:1.0
Listing Containers
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Show only container IDs
docker ps -q
# Format output
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
Inspecting and Logging
# View container logs
docker logs my-nginx
# Follow logs in real time
docker logs -f my-nginx
# Show last 50 lines
docker logs --tail 50 my-nginx
# Show timestamps
docker logs -t my-nginx
# Detailed container metadata (IP, mounts, env vars, etc.)
docker inspect my-nginx
# Extract a specific field with Go template
docker inspect --format '{{.NetworkSettings.IPAddress}}' my-nginx
# Real-time resource usage (CPU, memory, network, disk)
docker stats
# Running processes inside a container
docker top my-nginx
Executing Commands in Running Containers
# Open a shell in a running container
docker exec -it my-nginx bash
# Run a one-off command
docker exec my-nginx nginx -t
# Run as root even if container runs as another user
docker exec -u root -it my-nginx bash
Stopping and Removing
# Graceful stop (SIGTERM, then SIGKILL after 10s)
docker stop my-nginx
# Immediate kill
docker kill my-nginx
# Start a stopped container
docker start my-nginx
# Restart
docker restart my-nginx
# Remove a stopped container
docker rm my-nginx
# Force remove a running container
docker rm -f my-nginx
# Remove all stopped containers
docker container prune
# Stop and remove all containers (careful in production!)
docker stop $(docker ps -q) && docker rm $(docker ps -aq)
Port Mapping
Container ports are private by default. Use -p to expose them:
# -p hostPort:containerPort
docker run -d -p 8080:80 nginx # http://localhost:8080 → container:80
docker run -d -p 3000:3000 myapp # same port on both sides
docker run -d -p 127.0.0.1:5432:5432 postgres # bind to localhost only
# Let Docker assign a random host port
docker run -d -P nginx
# See which ports are mapped
docker port my-nginx
Copying Files
# Copy from container to host
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf
# Copy from host to container
docker cp ./nginx.conf my-nginx:/etc/nginx/nginx.conf
System Cleanup
# Remove everything unused (containers, images, networks, build cache)
docker system prune
# Include unused volumes too
docker system prune --volumes
# Check disk usage
docker system df Frequently Asked Questions
What is the difference between docker stop and docker kill?
docker stop sends SIGTERM to the process, giving it time to shut down gracefully (default 10s timeout before SIGKILL). docker kill sends SIGKILL immediately. Always prefer docker stop unless the container is frozen.
Why do my container changes disappear when I restart it?
Containers are ephemeral. Changes to the container filesystem are lost on removal. Use volumes to persist data outside the container.
What does the -d flag do?
Detached mode — runs the container in the background and returns you to the shell. Without -d, the container runs in the foreground and you see its output directly.