Docker: the beginner's cheatsheet
In the past few months I have started working with Docker. Initially I found that there was a lot to remember in terms of Docker’s key concepts and terminology, as well as all of the CLI commands and what each of them do.
I have created this cheatsheet to help with remembering the key Docker concepts and terminology, as well as what I have found to be the most useful Docker CLI and Docker Compose commands. Keep in mind that the list of commands give here contains just the most useful commands for those who are starting out with Docker, and is by no means exhaustive. You can find a full list of Docker commands here.
Also, I learned most of what is written in this article by doing the following Udemy course, (affiliate link) Docker and Kubernetes: The Complete Guide, which I recommend enroling on if you are looking to advance your knowledge of Docker further.
These are the key concepts of Docker explained at a high level.
Docker Server (aka Docker Daemon)
The Docker Server (or Docker Daemon) is a process which runs in the background and is in charge of creating and manipulating docker objects such as containers and images. Commands entered via the Docker Client are sent to and handled by the Docker Server.
The Docker Client is a CLI tool through which commands are delivered to the Docker Server.
An image is a Snapshot of a filesystem along with a startup command. Used as a blueprint for creating and running containers.
A container is an instance of an image, created using that image as a blueprint.
A container has access to a portion of its host system’s resources - on Linux this is achieved using Namespacing and Control Groups, on other operating systems such as Windows and Mac a Linux virtual machine is installed alongside Docker, and containers are run inside of this virtual machine.
A file which contains a set of instructions for building a Docker image.
A volume is a way of persisting data beyond the lifecycle of a Docker container.
Essentially, a volume allows you to map a folder on the filesystem within a Docker Container to a folder external to the container.
Docker Compose is used for working with and running multi-container Docker applications.
You may wonder why it is necessary for an application to be split across multiple containers - it is because many applications are made up of multiple components which communicate over a network - eg. an API running on a Node.js server, which communicates with a database running on its own database server. Docker Compose makes it easy to run each of these components in their own container, and create and configure a network over which these components can communicate.
Docker CLI commands
These are some of the most useful commands for working with the Docker CLI.
These are general commands that are useful when working with Docker:
Display version information of Docker Client and Docker Server.
docker ps --all
List all running containers on your machine - this can be useful to obtain the ID of a running container.
--allflag lists all containers which exist on your machine, as opposed to just running containers.
docker cp [sourceFilePath] [containerId]:[destinationFilePath]
Copy a file or folder from your local machine into a Docker container.
Commands for working with containers
These are commands for creating and working with Docker containers.
docker run [imageName]
The Docker Server will check your local image cache for the specified image. If the image is not found in the local image cache, it will then search for it on Docker Hub, and if found, download it and store it in your local image cache. Docker Server will then use the image as a sort of blueprint to create a container, and then start that container using the image’s startup command.
Note that running this command is equivalent to running the following 2 commands:
docker create [imageName]
docker start -a [containerId]
docker run [imageName] [command]
Same as the above, but allows us to overrun the startup command with the specified command.
docker create [imageName]
docker run [imageName], but will only create a container, but not start it.
docker start -a [containerId]
Start a container which has already been created (and may have already been started and then stopped), by specifying its ID.
The startup command from the image used to create the container will be used (note that
docker startcannot be used to override this startup command). The
-atells docker to ‘attach’ to the container, and print any output from the container to the terminal.
Note that in the above, and whenever you are working with container IDs, you do not need to use the full id - just enough of it that the section of the ID supplied is unique amongst existing container IDs.
docker system prune
Clean up disk space on your machine by removing stopped containers, as well as other bits and pieces such as unused networks, dangling images, and the Docker build cache. You can verify that this has removed all stopped containers by running
docker ps --allafterwards.
docker logs [containerId]
Output all of the logs from a specified container to the terminal.
docker stop [containerId]
Stop a container - Send a SIGTERM message to the process running inside the specified container (SIGTERM gives the processs a chance to clean itself up before stopping). If after 10 seconds, the process has not stopped, a SIGKILL will be ssent to the process to kill it.
docker kill [containerId]
Kill a container - Send a SIGTERM message to the process running inside the specified container.
docker exec -it [containerId] [command],
docker exec -it [containerId] sh
Execute a command inside the specific container.
-it, is actually 2 flags:
-iwires up anything you type to the STDIN of the container.
-tformats the output of the STDOUT from the container, in order to display it in a readable format in your terminal.
Running this with
shas the commandmis often very useful. It will start a command line shell inside of the container, allowing you to then execute whatever commands you wish inside the container.
docker run -it [imageName] sh
Create and start a container, running the
shcommand prompt as the startup command (you can then interact with the container via the command prompt).
docker inspect [containerId]
Inspect a container to obtain low level information about it. This command outputs a large JSON object, and if we just want to inspect some specific property we can use the
docker inspect -f '' [containerId], will output information about the container’s volumes.
Commands for building images
These commands are concerned with building images using a Dockerfile:
docker build [buildContext]
This command needs to be run in a folder containing a Dockerfile. It builds an image using the Dockerfile, using the specified folder path as the build context.
docker build -t [yourDockerId]/[projectName]:[version] [buildContext]
-ttag with docker build allows you to tag an image - as a convention, the above format is used for the tag.
docker build -f [yourDockerFile] [buildContext]
-ftag with docker build allows you specify a Dockerfile to user for the build (by default
Docker Compose commands
These commands are concerned with creating and running containers as specified inside of a
Create and run any containers and networks specified in
docker-compose.yml. By adding the
--buildflag, any rebuildable images that were used will be rebuilt before creating and running the containers.
docker-compose down -v
This will stop and remove containers and networks created by
docker-compose up. The
-vflag specifies that volumes should also be removed.
docker-compose run [service]
Create and run a container for the specified service within a
The following course has been very useful to me, and is a great way to get started with Docker (please note that the below is an affiliate link):