The Ultimate Docker Cheatsheet
Disclaimer: The details in this post have been derived from various sources. If you find any inaccuracies or omissions, please leave a comment, and I will do my best to fix them.
Docker has revolutionized the way we build, share, and deploy applications. Whether you’re just starting with Docker or you’re looking for a quick reference, this guide will provide you with everything you need to know about Docker commands, best practices, and tips.
Table of Contents
- What is Docker?
- Key Docker Concepts
- Installing Docker
- Essential Docker Commands
- Working with Images
- Working with Containers
- Docker Networks
- Docker Volumes (Data Persistence)
- Docker Compose
- Best Practices
- Troubleshooting Commands
- Resources for Further Learning
What is Docker?
Docker is a platform for developing, shipping, and running applications inside lightweight, portable containers. These containers ensure consistency across different environments, making it easier to scale and deploy applications.
Key Docker Concepts
- Image: A blueprint for containers, similar to a snapshot or template.
- Container: A running instance of an image.
- Dockerfile: A text file with instructions to build an image.
- Registry: A repository to store and share Docker images (e.g., Docker Hub).
- Volume: A mechanism to persist data generated or used by Docker containers.
- Network: Allows containers to communicate with each other or with external systems.
Installing Docker
Install Docker by following the official documentation for your operating system:
Verify installation:
docker --version
Essential Docker Commands
Check Docker Installation
docker version  # Display Docker client and server versions
docker info     # Display system-wide information
Get Help
docker --help        # General help
docker <command> --help  # Help for a specific command
Working with Images
Search and Pull Images
docker search <image_name>   # Search for an image on Docker Hub
docker pull <image_name>     # Download an image
List Local Images
docker images
Remove an Image
docker rmi <image_name>
Build an Image
docker build -t <image_name>:<tag> .
Working with Containers
Run a Container
docker run -it <image_name>        # Interactive terminal
docker run -d <image_name>         # Detached mode
docker run -p 8080:80 <image_name> # Map host port 8080 to container port 80
List Running Containers
docker ps          # Running containers
docker ps -a       # All containers
Stop, Start, and Restart a Container
docker stop <container_id>
docker start <container_id>
docker restart <container_id>
Remove a Container
docker rm <container_id>
Execute Commands Inside a Container
docker exec -it <container_id> bash
Inspect a Container
docker inspect <container_id>
Docker Networks
List Networks
docker network ls
Create a New Network
docker network create <network_name>
Connect a Container to a Network
docker network connect <network_name> <container_name>
Disconnect a Container from a Network
docker network disconnect <network_name> <container_name>
Docker Volumes (Data Persistence)
Create a Volume
docker volume create <volume_name>
List Volumes
docker volume ls
Use a Volume
docker run -v <volume_name>:/path/in/container <image_name>
Remove a Volume
docker volume rm <volume_name>
Docker Compose
What is Docker Compose?
Docker Compose allows you to define and manage multi-container applications using a docker-compose.yml file.
Common Commands
docker-compose up         # Start services
docker-compose down       # Stop services
docker-compose build      # Build images
docker-compose ps         # List services
Example docker-compose.yml
version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
Best Practices
- Minimize Image Size: Use lightweight base images like alpine.
- Use Multi-Stage Builds: Optimize builds for production.
- Tag Images: Use meaningful tags for versioning (myapp:v1.0).
- Leverage .dockerignore: Exclude unnecessary files when building images.
- Keep Containers Stateless: Store persistent data in volumes.
- Monitor Containers: Use tools like docker statsor external monitoring tools.
- Regular Cleanup: Remove unused containers, images, and volumes (docker system prune).
Troubleshooting Commands
Check Logs
docker logs <container_id>
Debugging Containers
docker exec -it <container_id> bash
Inspect Networking
docker network inspect <network_name>
Prune Unused Resources
docker system prune -a
Resources for Further Learning
This blog post is your all-in-one Docker guide. Bookmark it, and keep it handy for your containerization journey! Happy Dockering!