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
docker build -t <image_name> .
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.
Build Without Cache
docker build -t <image_name> . --no-cache
Forces a fresh build, ignoring cached layers. Useful when you want to ensure all dependencies are re-evaluated.
List Local Images
docker images
Displays all images stored locally on your machine.
Delete an Image
docker rmi <image_name>
Removes a specific image. Ensure no containers are using it first!
Remove All Unused Images
docker image prune
Cleans up unused images, freeing up disk space.
Tag an Image
docker tag <source_image> <target_image>
Creates a new tag for an existing image. For example, docker tag my-app:latest my-app:1.0
assigns the tag 1.0
to the my-app
image. Useful for versioning or preparing images for Docker Hub.
Practical Tip: Always tag images with specific versions before pushing to Docker Hub to avoid overwriting latest.
Inspect an Image
docker image inspect <image_name>
Displays detailed metadata about an image, such as its layers, environment variables, and configuration. For example, docker image inspect nginx
shows details about the NGINX image.
Practical Tip: Use this to verify image configurations or troubleshoot issues with dependencies.
Remove All Images
docker rmi -f $(docker images -q)
Deletes all images on your system. The -q
flag lists only image IDs, and -f
forces removal even if images are in use. Use with caution!
Practical Tip: Run docker images
first to review what will be deleted, and ensure no running containers depend on these images.
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 ..
** Clear Build Cache **
Sometimes Docker’s build cache can cause issues. Clear it by running:
docker builder prune -f
Practical Tip: Use this after updating dependencies in your Dockerfile
to ensure a clean build.
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
docker login -u <username>
Authenticates you to Docker Hub. Replace with your Docker Hub username.
Publish an Image
docker push <username>/<image_name>
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
docker pull <image_name>
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
docker --help
Displays help for Docker commands. Append --help
to any subcommand (e.g.,docker run --help
) for details.
System-Wide Information
docker info
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.
Check Docker Version
docker version
Displays the Docker client and server versions. Useful for ensuring compatibility or troubleshooting.
Practical Tip: Run this to confirm you’re using the latest Docker version before reporting issues.
Managing Containers
Containers are runtime instances of Docker images. They’re where your applications come to life. Here’s how to control them:
docker run --name <container_name> <image_name>
Create and Run a Container
docker run --name <container_name> <image_name>
Creates and starts a container with a custom name. For example, docker run --name my-app-container my-app.
Run with Port Mapping
docker run -p <host_port>:<container_port> <image_name>
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
docker run -d <image_name>
Starts a container in detached mode (runs in the background).
Start or Stop a Container
docker start <container_name>
docker stop <container_name>
Remove a Stopped Container
docker rm <container_name>
Deletes a stopped container.
Open a Shell in a Running Container
docker exec -it <container_name> sh
Opens an interactive shell inside a running container. Use bash instead of sh
if the container supports it.
View Container Logs
docker logs <container_name>
Shows logs for a container. Use -f
to follow logs in real-time
List All Containers
docker ps --all
Lists all containers, including stopped ones.
View Resource Usage
docker container stats
Displays real-time resource usage (CPU, memory, etc.) for running containers.
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 Dev.to community grow!
Happy containerizing! 🚢
Top comments (0)