Docker Cheatsheet: Quick Reference, Examples, and Pro Tips
Introduction: Docker has transformed application deployment and management with its lightweight, portable containers. To help you navigate Docker's vast array of commands and concepts, we've created a Docker cheatsheet infused with real-world examples, and valuable pointers. This cheatsheet will become your trusty companion as you sail through the Docker seas.
Docker Basics ๐ณ
Run a Container:
docker run <image-name>
Example:
docker run nginx
List Containers:
docker ps
Pro Tip: Use
-a
to list all containers, including stopped ones.Stop and Remove a Container:
docker stop <container-id> docker rm <container-id>
Example:
docker stop my_container
Docker Images ๐๏ธ
List Images:
docker images
Pull an Image:
docker pull <image-name>
Example:
docker pull ubuntu
Build an Image:
docker build -t <image-name> <path-to-Dockerfile>
Example:
docker build -t my_app .
Docker Volumes ๐
Create a Volume:
docker volume create <volume-name>
Example:
docker volume create data_volume
Run Container with a Volume:
docker run -v <volume-name>:<container-path> <image-name>
Example:
docker run -v data_volume:/app/data my_app
Docker Networks ๐
Create a Network:
docker network create <network-name>
Example:
docker network create my_network
Run Container in a Network:
docker run --network <network-name> <image-name>
Example:
docker run --network my_network my_app
Docker Compose ๐
Run Compose Services:
docker-compose up
Pro Tip: Use
-d
for detached mode.Scale Services:
docker-compose up --scale <service-name>=<num-instances>
Example:
docker-compose up --scale app=3
Stop Compose Services:
docker-compose down
Docker Cleaning ๐งน
Remove All Containers:
docker rm -f $(docker ps -aq)
Remove All Images:
docker rmi -f $(docker images -aq)
Pro Tips ๐ก
Use Aliases: Create aliases for frequently used commands in your shell profile for faster execution.
Tagging Images: Tag your images with versions (
-t my_app:v1
) for better organization.Docker Hub: Explore Docker Hub for pre-built images and community-contributed images.
Dockerfile Best Practices: Keep your Dockerfiles minimal, use .dockerignore to exclude unnecessary files, and prioritize layers to optimize build times.
Conclusion: Docker, with its immense capabilities, is made more accessible with this emoji-infused cheatsheet. Armed with practical examples and insightful tips, you're ready to navigate Docker's ocean of commands with ease. From containers to networks, images to volumes, and beyond, Docker empowers you to orchestrate applications effortlessly. Happy Dockerizing! ๐ข๐