Docker Containers
A Docker container is a runtime instance of a Docker image. A container runs as an isolated process on the host.
Container Lifecycle
Created → Running → Paused → Stopped → Removed
↑ ↓ ↓ ↓
└─────────┴────────┴─────────┘
Basic Container Commands
# Run a new container
docker run -d --name mycontainer nginx
# View running containers
docker ps
# View all containers (including stopped ones)
docker ps -a
# Stop a container
docker stop mycontainer
# Start a stopped container
docker start mycontainer
# Restart a container
docker restart mycontainer
# Delete a container
docker rm mycontainer
Running Containers
# Run with port mapping
docker run -d -p 8080:80 --name webserver nginx
# Run with a volume
docker run -d -v /host/path:/container/path --name data nginx
# Run with an environment variable
docker run -d -e POSTGRES_PASSWORD=secret --name db postgres
# Run interactively
docker run -it ubuntu bash
Accessing Containers
# Enter a running container
docker exec -it mycontainer bash
# Run a command inside a container
docker exec mycontainer ls -la
# View a container's logs
docker logs mycontainer
# View logs in real time
docker logs -f mycontainer
Resource Management
# Limit CPU
docker run -d --cpus="1.5" myapp:latest
# Limit memory
docker run -d --memory="512m" myapp:latest
# View resource usage
docker stats
Container Networking
# Run with a specific port
docker run -d -p 8080:80 nginx
# Run on a specific network
docker run -d --network mynetwork --name api myapp
# View port mapping
docker port mycontainer
Use Case: Application Containers
# Run a PostgreSQL database
docker run -d \
--name myapp-db \
-e POSTGRES_DB=myapp \
-e POSTGRES_USER=admin \
-e POSTGRES_PASSWORD=secret123 \
-v myapp-db-data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:15
# Run the backend application
docker run -d \
--name myapp-api \
--link myapp-db:database \
-e DB_HOST=database \
-e DB_PORT=5432 \
-e DB_NAME=myapp \
-e DB_USER=admin \
-e DB_PASSWORD=secret123 \
-p 3000:3000 \
myapp-api:1.0
# Run the frontend
docker run -d \
--name myapp-frontend \
--link myapp-api:api \
-e API_URL=http://api:3000 \
-p 8080:80 \
myapp-frontend:1.0
Monitoring Containers
# View real-time stats
docker stats
# View processes inside a container
docker top mycontainer
# View container inspect data
docker inspect mycontainer
# View disk usage
docker system df
Everyday Use Case
Case: A developer needs to run the application for testing.
# Run the application with a database
docker run -d \
--name test-app \
-p 3000:3000 \
-e NODE_ENV=development \
-v $(pwd):/app \
-w /app \
node:18-alpine \
npm run dev
# Access the application
curl http://localhost:3000
# View logs
docker logs -f test-app
Cleanup
# Delete stopped containers
docker container prune
# Delete all containers
docker rm -f $(docker ps -aq)
# Delete everything (containers, images, volumes)
docker system prune -a --volumes
Best Practices
- Use --name: Give descriptive names
- Restart policy: Use
--restart unless-stoppedfor production - Health checks: Define a health check for monitoring
- Resource limits: Set CPU and memory limits
- Don't run as root: Use
--userwhen possible
Use Case: Health Check
# Dockerfile
FROM node:18-alpine
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
# Run with a health check
docker run -d \
--name myapp-api \
--health-cmd="curl -f http://localhost:3000/health || exit 1" \
--health-interval=30s \
--health-timeout=3s \
-p 3000:3000 \
myapp-api:1.0
Conclusion
Containers are the basic unit in Docker. Understanding how to run, manage, and monitor containers is essential for managing applications that run in containers.