π³ Docker Cheat Sheet: From Beginner to Expert
Docker makes it easy to build, ship, and run applications in containers. This cheat sheet is a progressive guide β starting from the basics and moving into advanced concepts.
πΉ 1. Basics
Docker
A platform to run applications in isolated environments called containers. Think of it as lightweight virtual machines.
Image
A blueprint (snapshot) of your app β contains code, dependencies, and environment setup.
Container
A running instance of an image. You can start/stop/delete it.
Docker Hub
A public registry of prebuilt images (nginx
, mysql
, node
, etc.).
πΉ 2. Installation & Setup
docker --version # Check Docker version
docker info # System info (containers, images, storage driver, etc.)
docker login # Login to Docker Hub
πΉ 3. Working with Images
docker pull nginx # Download image from Docker Hub
docker images # List images
docker rmi nginx # Remove image
docker build -t myapp . # Build image from Dockerfile
-
-t myapp
β Tags the image with a name. -
.
β Uses current directory (Dockerfile must be here).
πΉ 4. Working with Containers
docker run hello-world # Run a test image
docker ps # List running containers
docker ps -a # List all containers (even stopped)
docker stop <id> # Stop a container
docker rm <id> # Remove a container
π Useful Flags for docker run
:
-
-d
β Run in background (detached). -
-p 8080:80
β Map host port to container port. -
--name mycontainer
β Give custom name.
Example:
docker run -d -p 8080:80 nginx
πΉ 5. Inspect & Logs
docker logs <id> # View container logs
docker exec -it <id> bash # Access shell inside container
docker inspect <id> # Detailed info (JSON)
πΉ 6. Dockerfile (Build Custom Images)
A Dockerfile
defines how to build your image. Example:
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "server.js"]
π Commands used:
- FROM β Base image.
- WORKDIR β Working directory inside container.
- COPY β Copy files from host to container.
- RUN β Execute commands (install, setup).
- CMD β Default command when container starts.
πΉ 7. Volumes (Persistent Storage)
Containers are ephemeral (data is lost if removed). Volumes persist data.
docker volume create mydata
docker run -v mydata:/var/lib/mysql mysql
π Bind mount example:
docker run -v $(pwd):/app myapp
(Mounts current folder into container).
πΉ 8. Networking
docker network ls # List networks
docker network create mynetwork # Create custom network
docker run --network=mynetwork ...
π Allows containers to communicate with each other.
πΉ 9. Docker Compose (Multi-Container Apps)
Instead of running multiple containers manually, use docker-compose.yml
:
version: "3"
services:
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
web:
build: .
ports:
- "8080:80"
depends_on:
- db
Run with:
docker compose up -d
docker compose down
πΉ 10. Advanced Commands
docker stats # Real-time CPU/memory usage
docker system prune # Clean up unused data
docker save -o myimage.tar app # Save image to tar file
docker load -i myimage.tar # Load image from tar file
docker cp <id>:/file . # Copy file from container
Top comments (1)
Very handy!
I have something to add. When docker has eaten a lot of the memory, you can use
docker system prune
to clean up the stopped containers, dangling images and build cache. You could add-a
and--volumes
flags. But be cautious with them!