Skip to content

Docker_containers

Containers are running instances of Docker images. This chapter covers creating, running, and managing Docker containers.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Container Lifecycle │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ create ┌─────────┐ start ┌─────────┐ │
│ │ Image │ ──────────────▶ │ Created │ ──────────────▶ │ Running │ │
│ └─────────┘ └─────────┘ └────┬────┘ │
│ │ │
│ ┌─────────┐ stop │ │ run │
│ │ Stopped │ ◀──────────────┤ │ process │
│ └─────────┘ │ │ │
│ │ │ │
│ ┌─────────┐ kill │ ▼ │
│ │ Dead │ ◀───────────────┤ ┌─────────┐ │
│ └─────────┘ └───────────────────── │ Running │ │
│ │ with │ │
│ │ PID 1 │ │
│ └─────────┘ │
│ │
│ Container States: Created → Running → Paused → Stopped → Deleted │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# Run a container in interactive mode
docker run -it ubuntu /bin/bash
# Run in detached mode (background)
docker run -d nginx
# Run with custom name
docker run -d --name my-nginx nginx
# Run with port mapping
docker run -d -p 8080:80 nginx
# Run with volume mount
docker run -d -v /myapp:/app myapp
Terminal window
# Interactive with pseudo-TTY
docker run -it ubuntu bash
# Detached (background)
docker run -d nginx
# Remove container when it exits
docker run --rm ubuntu echo "Hello"
# Assign random host port
docker run -d -P nginx
# Set environment variables
docker run -d -e DATABASE_URL=postgres://db:5432/app myapp
# Set container hostname
docker run -d --hostname mycontainer nginx
Terminal window
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# List containers with full IDs
docker ps -a --no-trunc
# Format output
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
# Filter containers
docker ps --filter "status=exited"
docker ps --filter "name=nginx"
Terminal window
# Start a stopped container
docker start mycontainer
# Stop a running container
docker stop mycontainer
# Restart a container
docker restart mycontainer
# Pause container processes
docker pause mycontainer
# Unpause container processes
docker unpause mycontainer
# Kill container (SIGKILL)
docker kill mycontainer
# Remove container
docker rm mycontainer
# Force remove running container
docker rm -f mycontainer
Terminal window
# View container details
docker inspect mycontainer
# View container logs
docker logs mycontainer
# Follow logs in real-time
docker logs -f mycontainer
# View last N lines of logs
docker logs --tail 100 mycontainer
# View logs with timestamps
docker logs -t mycontainer
# Execute command in running container
docker exec -it mycontainer bash
# Run additional process in container
docker exec mycontainer ls -la
Terminal window
# Map container port to host port
docker run -d -p 8080:80 nginx # Host 8080 → Container 80
docker run -d -p 8081:80 nginx # Multiple containers
# Map to specific IP
docker run -d -p 127.0.0.1:8080:80 nginx
# Map to random available port
docker run -d -P nginx
# View port mappings
docker port mycontainer
┌─────────────────────────────────────────────────────────────────────────────┐
│ Docker Network Types │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────┐ │
│ │ Bridge │ Default network for standalone containers │
│ │ │ Containers on same bridge can communicate │
│ └──────────────────┘ │
│ │
│ ┌──────────────────┐ │
│ │ Host │ Container shares host's network namespace │
│ │ │ No network isolation from host │
│ └──────────────────┘ │
│ │
│ ┌──────────────────┐ │
│ │ Overlay │ Multi-host networking (Docker Swarm) │
│ │ │ Containers across hosts can communicate │
│ └──────────────────┘ │
│ │
│ ┌──────────────────┐ │
│ │ None │ No networking - completely isolated │
│ └──────────────────┘ │
│ │
│ ┌──────────────────┐ │
│ │ Macvlan │ Container gets MAC address on physical network │
│ │ │ Appears as physical device on network │
│ └──────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# Set memory limit
docker run -d --memory 512m nginx
# Set memory reservation
docker run -d --memory-reservation 256m nginx
# Set memory and swap limit
docker run -d --memory 512m --memory-swap 1g nginx
Terminal window
# Limit CPU to 1 core
docker run -d --cpus=1 nginx
# Limit to specific cores
docker run -d --cpuset-cpus=0,1 nginx
# Limit CPU share (default 1024)
docker run -d --cpu-shares 512 nginx
Terminal window
# View container stats (real-time)
docker stats
# View stats for specific container
docker stats mycontainer
# View stats in non-streaming mode
docker stats --no-stream mycontainer
Terminal window
# View logs
docker logs mycontainer
# Follow logs
docker logs -f mycontainer
# View last 50 lines
docker logs --tail 50 mycontainer
# View logs with timestamps
docker logs -t mycontainer
# Since timestamp
docker logs --since 2024-01-01T10:00:00 mycontainer
# View only stderr or stdout
docker logs --stderr mycontainer
docker logs --stdout mycontainer
Terminal window
# Remove stopped containers
docker container prune
# Remove all stopped containers
docker rm $(docker ps -aq)
# Remove all containers (including running)
docker rm -f $(docker ps -aq)
# Remove containers older than 24 hours
docker container prune --filter "until=24h"

Docker can monitor container health using health checks:

FROM nginx:latest
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost/ || exit 1
Terminal window
# Check container health status
docker inspect --format='{{.State.Health.Status}}' mycontainer
Terminal window
# Set environment variable
docker run -d -e MY_VAR=value nginx
# Set multiple variables
docker run -d -e DB_HOST=db -e DB_PORT=5432 myapp
# Pass from host environment
docker run -d -e HOST_VAR myapp
# Use .env file
docker run -d --env-file .env myapp
Terminal window
# Override entrypoint
docker run -d --entrypoint /bin/sh myapp
# Run with arguments
docker run myapp arg1 arg2
Terminal window
docker run -d \
--name nginx-web \
-p 80:80 \
-v /var/www/html:/usr/share/nginx/html:ro \
nginx:latest
Terminal window
docker run -d \
--name postgres-db \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=mydb \
-v postgres-data:/var/lib/postgresql/data \
postgres:15
Terminal window
docker run -it \
--name dev-env \
-v $(pwd):/workspace \
-p 3000:3000 \
node:18 \
bash
Terminal window
# Run container with interactive shell
docker run -it --rm ubuntu bash
# Inspect running process
docker top mycontainer
# Get detailed container info
docker inspect mycontainer
# Check container events
docker events
# Copy files from container
docker cp mycontainer:/app/logs ./logs

In this chapter, you learned:

  • Container lifecycle and states
  • Running containers with various options
  • Container management commands
  • Network configuration
  • Resource limits and monitoring
  • Logging and debugging