Appearance
Docker Commands
Comprehensive Docker commands and workflows for containerization across all platforms.
Basic Commands
Command | Description |
---|---|
docker --version | Show Docker version |
docker info | Display system-wide information |
docker help | Show help for Docker commands |
docker images | List all images |
docker ps | List running containers |
docker ps -a | List all containers (running and stopped) |
docker pull <image> | Pull an image from registry |
docker run <image> | Run a container from image |
docker stop <container> | Stop a running container |
docker start <container> | Start a stopped container |
docker restart <container> | Restart a container |
docker rm <container> | Remove a container |
docker rmi <image> | Remove an image |
Container Management
Command | Description |
---|---|
docker run -d <image> | Run container in detached mode |
docker run -it <image> | Run container interactively |
docker run -p 8080:80 <image> | Map port 8080 to container port 80 |
docker run -v /host:/container <image> | Mount volume |
docker run --name <name> <image> | Run container with custom name |
docker exec -it <container> bash | Execute bash in running container |
docker logs <container> | View container logs |
docker logs -f <container> | Follow container logs |
docker inspect <container> | Inspect container details |
docker stats | Show container resource usage |
docker top <container> | Show running processes in container |
Image Management
Command | Description |
---|---|
docker build -t <name> . | Build image from Dockerfile |
docker build -t <name>:<tag> . | Build image with tag |
docker tag <image> <new-name> | Tag an image |
docker push <image> | Push image to registry |
docker save <image> > file.tar | Save image to tar file |
docker load < file.tar | Load image from tar file |
docker history <image> | Show image history |
docker search <term> | Search for images in registry |
Docker Compose
Command | Description |
---|---|
docker-compose up | Start services defined in compose file |
docker-compose up -d | Start services in detached mode |
docker-compose down | Stop and remove containers |
docker-compose build | Build or rebuild services |
docker-compose logs | View logs for all services |
docker-compose ps | List containers |
docker-compose exec <service> bash | Execute command in service |
docker-compose pull | Pull latest images |
docker-compose restart | Restart services |
Network Management
Command | Description |
---|---|
docker network ls | List networks |
docker network create <name> | Create a network |
docker network rm <name> | Remove a network |
docker network inspect <name> | Inspect network details |
docker run --network <name> <image> | Run container on specific network |
Volume Management
Command | Description |
---|---|
docker volume ls | List volumes |
docker volume create <name> | Create a volume |
docker volume rm <name> | Remove a volume |
docker volume inspect <name> | Inspect volume details |
docker volume prune | Remove unused volumes |
Common Workflows
Development Workflow
bash
# Build and run application
docker build -t myapp .
docker run -p 3000:3000 myapp
# Development with volume mounting
docker run -p 3000:3000 -v $(pwd):/app myapp
# Using Docker Compose for development
docker-compose up --build
Production Deployment
bash
# Build production image
docker build -t myapp:prod .
# Tag for registry
docker tag myapp:prod registry.com/myapp:latest
# Push to registry
docker push registry.com/myapp:latest
# Deploy on production server
docker pull registry.com/myapp:latest
docker run -d -p 80:3000 --name myapp-prod registry.com/myapp:latest
Container Debugging
bash
# Check container logs
docker logs -f container-name
# Execute shell in running container
docker exec -it container-name bash
# Inspect container configuration
docker inspect container-name
# Check resource usage
docker stats container-name
Best Practices
Dockerfile Optimization
- Use multi-stage builds to reduce image size
- Leverage build cache by ordering instructions properly
- Use specific base image tags, avoid
latest
- Minimize the number of layers
- Use
.dockerignore
to exclude unnecessary files
Security
- Run containers as non-root user
- Use official base images when possible
- Regularly update base images
- Scan images for vulnerabilities
- Limit container resources (CPU, memory)
Performance
- Use appropriate base images (alpine for smaller size)
- Optimize layer caching
- Use health checks for containers
- Monitor container resource usage
- Use volumes for persistent data
Production Deployment
- Use orchestration tools (Docker Swarm, Kubernetes)
- Implement proper logging and monitoring
- Use secrets management for sensitive data
- Set up automated backups
- Implement rolling updates