Docker cheat sheet for developers

Docker cheat sheet for developers

Docker is a containerization tool that enables you to deploy and configure services in isolated environments. To manage various aspects of this platform and the services running on top of it, you mainly use various Docker commands.

There are different Docker commands for specific tasks. Based on their purpose, they fall into these categories:

  • Build commands. Create an image from a Dockerfile.
  • Cleanup commands. Remove unused images and volumes to free up space.  
  • Container interaction commands. Manage and communicate with containers. 
  • Container inspection commands. Analyze and check the details of containers.
  • Image management commands. Administer images. 
  • Run commands. Build a container from an image and change its settings. 
  • Registry commands. Interact with a remote Docker image registry, like Docker Hub. 
  • Service commands. Manage all aspects of Docker services.
  • Network commands. Configure, manage, and interact with the Docker network. 

Let’s dive deeper into each category to learn the commands in more detail, their variations, and their purposes. At the end, you’ll find a Docker cheat sheet that you can download and easily look back to when unsure what utilities to use.

Build commands

The build command in Docker is used to build images from a Dockerfile. There are some variations of this command used for different tasks, with the most common ones including:

CommandExplanation
docker buildBuilds an image from a Dockerfile in the current directory
docker build https://github.com/docker/rootfs.git#container:dockerBuilds an image from a remote Git repository
docker build -t imagename/tagBuilds and tags an image for easier tracking
docker build https://yourserver/file.tar.gzBuilds an image from a remote tar archive
docker build -t image:1.0 -<<EOF FROM busybox RUN echo "hello world"EOFBuilds an image via a Dockerfile that is passed through STDIN or standard input

Cleanup commands

The cleanup commands, as the name suggests, remove unused images, containers, and volumes to keep your system clean, as well as freeing up storage space. Here are operations you can do with it:

CommandExplanation
docker image pruneDeletes dangling or untagged images
docker image prune -aClears all images that aren’t being used by containers
docker system pruneRemoves all stopped containers, all networks not used by containers, all dangling images, and all build cache
docker image rm image-nameRemoves a specific image by name
docker rm containerRemoves a running container
docker swarm leaveLeaves a Swarm
docker stack rm stacknameDeletes a Swarm
docker volume rm $(docker volume ls -f dangling=true -q)Removes all dangling volumes
docker rm $(docker ps -a -q)Clears all stopped containers
docker kill $(docker ps -q)Stops all running containers

Container interaction commands

The container interaction commands are used to build and manage applications running on isolated environments. Here are some of the most common ones:

CommandExplanation
docker start containerStarts a new container
docker stop containerStops a container
docker pause containerPauses a container
docker unpause containerUnpauses a container
docker restart containerRestarts a container
docker export containerExports container contents to a tar archive
docker attach containerAttaches to a running container
docker wait containerWaits until the container is terminated and shows the exit code
docker commit -m “commit message” -a "author" container username/image_name: tagSaves a running container as an image
docker logs -ft containerFollows container logs
docker exec -ti container script.shRuns a command in a container
docker commit container imageCreates a new image from a container
docker create imageCreates a new container from an image

Container inspection commands

The container inspection commands are helpful for checking details of the isolated environments, which is commonly done for quality assurance or troubleshooting purposes. Here are some commands that help you get an overview of what different containers are doing:

CommandExplanation
docker psLists all running containers
docker ps -aLists all containers, regardless of their statuses
docker diff containerInspects changes to directories and files in the container filesystem
docker top containerShows all running processes in an existing container
docker inspect containerDisplays low-level information about a container
docker logs containerGathers the logs for a container
docker stats containerShows container resource usage statistics

Image management commands

The image management commands enable you to modify and inspect images that will be used to build containers. The most common utilities for this purpose include:

CommandExplanation
docker image lsLists images
docker image rm mysqlRemoves an image
docker tag Tags an image
docker history imageDisplays the image history
docker inspect imageDisplays low-level information about an image

Run command

The run command in Docker is used to create containers from provided images. The default syntax for this utility looks like:

docker run (options) image (command) (arg...)

You can add the following flags to modify the command behavior and build containers using specific settings:

FlagExplanation
--detach , -dRuns the container in the background and prints the container ID
--env , -eSets environment variables for the container
--hostname , -hGives the container a specific hostname
--label , -lCreates a metadata label for the container
--nameAssigns a name to a container
--networkConnects the container to a Docker network
--rmRemoves container when it stops
--read-onlySets the container filesystem as read-only
--workdir , -wSets a working directory in the container

Registry commands

Docker registry commands enable you to interact with remote image repositories, such as the official Docker Hub or a self-hosted registry running on a private server:

CommandExplanation
docker loginLogs in to a registry
docker logoutLogs out from a registry
docker pull mysqlPulls an image from a registry
docker push repo/rhel-httpd:latestPushes an image to a registry
docker search termSearches Docker Hub for images with the specified term

Service commands

The service commands are used to manage a Docker Swarm service — an image for a microservice within the context of a larger application. These include:

CommandExplanation
docker service lsLists all services running in a swarm
docker stack services stacknameLists all running services
docker service ps servicenameLists the tasks of a service
docker service update servicenameUpdates a service
docker service create imageCreates a new service
docker service scale servicename=10Scales one or more replicated services
docker service logs servicenameLists all service logs

Network commands

The network commands are used to manage your Docker application’s network, which defines how containers communicate with each other and external systems. Here are some of the most common ones:

CommandExplanation
docker network create network-nameCreates a new network
docker network rm network-nameRemoves a specified network
docker network lsLists all networks
docker network connect network-name containerConnects a container to a network
docker network disconnect network-name containerDisconnects a container from a network
docker network inspect network-nameDisplays detailed information about a network

Understanding how to use Docker

Commands you have learned in this cheat sheet are used to interact with various entities in the broader Docker ecosystem, specifically the server, client, container, image, and registry.

If you’re new to Docker, master running and inspecting containers first (docker run, docker ps, docker logs, docker exec), then progress to building images (docker build, docker tag, docker push). Advanced networking and Swarm commands are only needed for specific deployment scenarios.

To learn these concepts in detail and understand their relationship to the commands, refer to our Docker tutorial

Remembering all the commands can be challenging so below you’ll find a downloadable Docker cheat sheet that you can download and print out for a handy reference when using the container platform. 

Download free docker cheat sheet

All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.

Author
The author

Ignas R.

Ignas takes great satisfaction in helping people tackle even the most complex technical issues. His current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house.

Author
The Co-author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.