Docker Cheatsheet: Quick Reference, Examples, and Pro Tips

ยท

2 min read

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 ๐Ÿณ

  1. Run a Container:

     docker run <image-name>
    

    Example: docker run nginx

  2. List Containers:

     docker ps
    

    Pro Tip: Use -a to list all containers, including stopped ones.

  3. Stop and Remove a Container:

     docker stop <container-id>
     docker rm <container-id>
    

    Example: docker stop my_container

Docker Images ๐Ÿž๏ธ

  1. List Images:

     docker images
    
  2. Pull an Image:

     docker pull <image-name>
    

    Example: docker pull ubuntu

  3. Build an Image:

     docker build -t <image-name> <path-to-Dockerfile>
    

    Example: docker build -t my_app .

Docker Volumes ๐Ÿ“‚

  1. Create a Volume:

     docker volume create <volume-name>
    

    Example: docker volume create data_volume

  2. 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 ๐ŸŒ

  1. Create a Network:

     docker network create <network-name>
    

    Example: docker network create my_network

  2. Run Container in a Network:

     docker run --network <network-name> <image-name>
    

    Example: docker run --network my_network my_app

Docker Compose ๐Ÿš€

  1. Run Compose Services:

     docker-compose up
    

    Pro Tip: Use -d for detached mode.

  2. Scale Services:

     docker-compose up --scale <service-name>=<num-instances>
    

    Example: docker-compose up --scale app=3

  3. Stop Compose Services:

     docker-compose down
    

Docker Cleaning ๐Ÿงน

  1. Remove All Containers:

     docker rm -f $(docker ps -aq)
    
  2. 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! ๐Ÿšข๐Ÿ‹

ย