Docker CLI Cheat Sheet: Your Quick Guide to Container Mastery
Docker has revolutionized how developers build, share, and run applications. By packaging applications into lightweight, portable containers, Docker ensures consistency across development, testing, and production environments. Whether you're new to Docker or a seasoned user looking for a quick reference, this cheat sheet, based on the official Docker CLI commands, will help you navigate Docker with ease. In this article, we’ll break down essential Docker commands, explain their purpose, and provide practical tips to get you started or refresh your knowledge. Let’s dive in!
Why Docker? A Quick Overview
Docker allows you to package an application with all its dependencies—code, runtime, system tools, libraries, and settings—into a container. Containers are isolated, lightweight, and run consistently regardless of the host environment. This makes them perfect for developing locally, sharing with teams, or deploying to production.
This cheat sheet covers the core Docker CLI commands for managing images, containers, Docker Hub, and more. Whether you’re building a new image or running a containerized app, these commands are your go-to tools.
Getting Started: Installing Docker
Before diving into the commands, you need Docker installed. Docker Desktop is available for Mac, Linux, and Windows—download it from the official Docker documentation. For inspiration, check out Docker’s Awesome Compose for sample projects, or explore the Docker docs for detailed guides.
Pro Tip: Ensure your system meets Docker’s requirements, and keep Docker updated to access the latest features and security patches.
Working with Docker Images
Docker images are the blueprints for containers. They’re standalone, executable packages containing everything an application needs to run. Here’s how to manage them:
Build an Image from a Dockerfile
Builds an image from a Dockerfile in the current directory (.). The -t flag assigns a name to the image. For example, docker build -t my-app . creates an image named my-app.
Forces a fresh build, ignoring cached layers. Useful when you want to ensure all dependencies are re-evaluated.
Build Without Cache
Forces a fresh build, ignoring cached layers. Useful when you want to ensure all dependencies are re-evaluated.
List Local Images
Displays all images stored locally on your machine.
Delete an Image
Removes a specific image. Ensure no containers are using it first!
Remove All Unused Images
Cleans up unused images, freeing up disk space.
Practical Tip: Tag your images with versions (e.g., my-app:1.0) to keep track of updates. For example: docker build -t my-app:1.0 ..
Leveraging Docker Hub
Docker Hub is the go-to platform for finding, sharing, and storing container images. It’s like GitHub for Docker images. Here’s how to interact with it:
Log In to Docker Hub
Authenticates you to Docker Hub. Replace <username> with your Docker Hub username.
Publish an Image
Uploads your image to Docker Hub. For example, docker push johndoe/my-app shares the my-app image
Search for an Image
docker search <image_name>
Finds images on Docker Hub. Try docker search nginx to explore available NGINX images.
Pull an Image
Downloads an image from Docker Hub to your local machine.
Pro Tip: Use official images (e.g., nginx, postgres) from Docker Hub for reliability and security. Always check the image’s documentation for configuration details.
General Docker Commands
These commands help you manage Docker itself:
Get Help
Displays help for Docker commands. Append --help to any subcommand (e.g., docker run --help) for details.
System-Wide Information
Shows Docker’s configuration, including running containers, images, and system details.
Practical Tip: Use docker info to troubleshoot issues, like checking if the daemon is running or verifying storage driver compatibility.
Managing Containers
Containers are runtime instances of Docker images. They’re where your applications come to life. Here’s how to control them:
Create and Run a Container
Creates and starts a container with a custom name. For example, docker run --name my-app-container my-app.
Run with Port Mapping
Maps a host port to a container port. For example, docker run -p 8080:80 nginx makes the NGINX container accessible at localhost:8080.
Run in Background
Starts a container in detached mode (runs in the background).
Start or Stop a Container
Remove a Stopped Container
Deletes a stopped container.
Open a Shell in a Running Container
Opens an interactive shell inside a running container. Use bash instead of sh if the container supports it.
View Container Logs
Shows all currently running containers.
List All Containers
Lists all containers, including stopped ones.
View Resource Usage
Practical Tip: Use docker ps and docker logs -f to monitor and debug containers. If a container exits unexpectedly, check its logs for clues.
Wrapping Up
This Docker CLI cheat sheet is your quick reference for managing images, containers, and Docker Hub. Whether you’re building a new image, sharing it with your team, or debugging a container, these commands will streamline your workflow. Docker’s power lies in its simplicity and consistency—master these commands, and you’ll be containerizing apps like a pro.
For more advanced topics, explore Docker Compose for multi-container setups or dive into the Docker documentation for in-depth guides. Have a favorite Docker tip or command? Share it in the comments below to help the community grow!
Happy containerizing! 🚢