Dec 08, 2023
Docker Cheat Sheet: All the Most Essential Commands in One Place + Downloadable PDF
Docker is a popular open-source platform that makes it easy to build, test, deploy, and manage containerized applications in a consistent, portable, or virtual environment such as VPS.
While a powerful tool in your development arsenal, learning the different Docker commands can take time and effort. New users often benefit from having a Docker cheat sheet readily at hand.
In this tutorial, we will explain how Docker works and provide the most common Docker commands, along with a downloadable cheat sheet for you to use.
Docker architecture consists of five main components: server, client, container, image, and registry.
A Docker server or Docker daemon is a program that runs in the background of your computer and manages Docker containers and images. When you use the Docker command line interface.
(CLI) to create, run, or manage containers, you interact with the Docker daemon.
The Docker daemon is an essential platform component that ensures containers can be started and stopped automatically when the system boots up.
The Docker client lets users interact with the Docker daemon with its command-line interface (CLI). In simple terms, it’s the main part of the Docker architecture for creating, managing, and running container applications.
When you use the Docker CLI to pass a command, the Docker client sends the command to the Docker daemon running on your computer, which then carries out the requested operation. The Docker client can be installed on any machine that needs to interact with the Docker daemon, including your local machine, a remote server, or a virtual server.
A Docker container is a package that contains all the required prerequisites to run an application.
Containers are designed to be highly portable, meaning that they can be easily moved from one environment to another, such as from a developer’s laptop to a testing environment or from a testing environment to a production environment.
A Docker image is a preconfigured template that specifies what should be included in a Docker container. Usually, images are downloaded from websites like Docker Hub. However, it’s also possible to create a custom image with the help of Dockerfile.
The Docker registry is a central repository that stores and manages Docker images. It is a server-based system that lets users store and share Docker images with others, making it easy to distribute and deploy applications. The most notable Docker registry is Docker Hub.
Docker Commands Cheat Sheet
Now that you know how Docker functions, let’s look at some of the most popular Docker command examples.
Docker uses the build command for building images from a Docker file. Some of the most common commands include:
|docker build||Builds an image from a Dockerfile in the current directory|
|docker build https://github.com/docker/|
|Builds an image from a remote GIT repository|
|docker build -t imagename/tag||Builds and tags an image for easier tracking|
|docker build https://yourserver/file.tar.gz||Builds an image from a remote tar archive|
|docker build -t image:1.0|
-<<EOFFROM busyboxRUN echo “hello world”EOF
|Builds an image via a Dockerfile that is passed through STDIN|
Clean Up Commands
To keep your system clean and save disk space, it’s a great idea to clean up unused images, containers, and volumes. Check the commands below for more details:
|Clears an unused image|
|Clears all images that are not being used by containers|
|Removes all stopped containers, all networks not used by containers, all dangling images, and all build cache|
|Removes an image|
|Removes a running container|
|Leaves a swarm|
|Removes a swarm|
|Removes all dangling volumes|
|Removes all stopped containers|
|Stops all running containers|
Container Interaction Commands
Interact with your Docker container with the following common commands:
|Starts a new container|
|Stops a container|
|Pauses a container|
|Unpauses a container|
|Restarts a container|
|Blocks a container|
|Exports container contents to a tar archive|
|Attaches to a running container|
|Waits until the container is terminated and shows the exit code|
|Saves a running container as an image|
|Follows container logs|
|Runs a command in a container|
|Creates a new image from a container|
|Creates a new container from an image|
Container Inspection Commands
Sometimes, you need to inspect your containers for quality assurance or troubleshooting purposes. These commands help you get an overview of what different containers are doing:
|Lists all running containers|
|Lists all containers|
|Inspects changes to directories and files in the container filesystem|
|Shows all running processes in an existing container|
|Displays low-level information about a container|
|Gathers the logs for a container|
|Shows container resource usage statistics|
Manage Images Commands
Some of the most common image management commands include:
|Removes an image|
|Tags an image|
|Displays the image history|
|Displays low-level information about an image|
Docker uses the run command to create containers from provided images. The default syntax for this command looks like this:
docker run (options) image (command) (arg...)
After the default syntax, use one of the following flags:
|Runs a container in the background and prints the container ID|
|Sets environment variables|
|Sets a hostname to a container|
|Creates a meta data label for a container|
|Assigns a name to a container|
|Connects a container to a network|
|Removes container when it stops|
|Sets the container filesystem as read-only|
|Sets a working directory in a container|
If you need to interact with Docker Hub, use the following commands:
|Logs in to a registry|
|Logs out from a registry|
|Pulls an image from a registry|
|Pushes an image to a registry|
|Searches Docker Hub for images with the specified term|
Manage all Docker services with these basic commands:
|Lists all services running in a swarm|
|Lists all running services|
|Lists the tasks of a service|
|Updates a service|
|Creates a new service|
|Scales one or more replicated services|
|Lists all service logs|
If you need to interact with the Docker network, use one of the following commands:
|Creates a new network|
|Removes a specified network|
|Lists all networks|
|Connects a container to a network|
|Disconnects a container from a network|
|Displays detailed information about a network|
Docker is a great tool for anyone willing to try out containers. The learning curve can be steep if you’re unfamiliar with container-based development. Luckily, having a cheat sheet at hand can speed up the process, as all common commands are easily reachable, and you don’t need to look them up on the internet.
In this tutorial, we’ve covered the basics of Docker architecture and gone through all the basic Docker commands, all of which can be found in our downloadable Docker cheat sheet.
We hope that you found this Docker tutorial useful. If you have any questions, leave them in the comments section below.