4th gen AMD EPYC coming soon Pre-order now

Docker Commands Cheat Sheet [With Examples]

December 19th, 2023
Docker Commands Cheat Sheet [With Examples]

Docker is one of the pillars of DevOps practices and is being used on nearly every modern Internet platform. In this article, you will learn the different Docker commands to streamline the development and deployment of your workflow.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers, seeing broad adoption in microservices development. Docker containers allow a developer to package up an application with all of its dependencies into a standardized unit for software development.

What are Docker commands?

Docker commands allow you to build, run, manage, and deploy Docker containers. For such complex projects, Docker provides a command line interface (CLI) that allows us to manage containers and images and perform general Docker tasks. The Docker CLI comes with around 60 commands, which can be grouped into four sections: managing containers, managing images, actions on Docker Hub, and general commands.

Prerequisites

To follow along and test these essential basic Docker commands, you will need:

  • The latest Ubuntu installed;
  • The latest Docker installed;
  • A system account with administrative privileges and sudo permissions.

Basic Docker commands cheat sheet

We will cover the different basic Docker commands and their basic usage. For a better overview, the commands in this tutorial are divided into the following seven categories:

Docker management commands; Docker Hub commands; Docker images command; Docker container commands; Docker network commands; Docker volume commands; Docker Swarm commands.

1. Docker management commands

The Docker management commands are general commands used to get system information or help from Docker CLI.

We will start with the command that lists all the other commands, docker --help.

docker --help

To get help from Docker.

docker --help

You can also use –help on all subcommands.

docker -d

To start the Docker daemon.

docker -d

docker info

To display Docker system-wide information.

docker info

docker version

To display the current installed Docker version.

docker version

2. Docker Hub commands

Docker Hub is a service provided by Docker that hosts container images. You can download and even push your own images in Docker Hub, making it accessible to everyone connected to the service.

The Docker Hub related commands are:

docker login

To login into Docker.

docker login -u <username>

docker push

To publish an image to Docker Hub.

docker push <username>/<image_name>

docker search

To search for an image in Docker Hub.

docker search <image_name>

For example, you can search for all images containing the work umami, which is a self-hosted analytic platform:

docker search umami
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
elestio/umami                  Umami, verified and packaged by Elestio         1
umamisoftware/umami            Umami is a simple, fast, privacy-focused alt…   3
pabloszx/umami                                                                 5
umami/docker-caddy             automated build of caddy .10                    0                    [OK]
sparanoid/umami                Mirror of ghcr.io/mikecao/umami due to the l…   3
btagging/umami                                                                 0
askask123/umami_arm                                                            0
umamichi/codebuild-s3-sample   This is samples code. Deploy to AWS S3 by Co…   1                    [OK]

The command also returns the image rating and whether it is an official image provided by the product developers.

docker pull

To pull an image from the Docker Hub.

docker pull is one of the most common Docker commands:

docker pull <image_name>

To run a container, we need to first pull the container and then run it.

Docker is also experiment with the Docker hub-tool to simplify the interaction with the Docker hub.

3. Docker images command

The following commands allow it to work with the Docker images.

docker build

To build an image from a Dockerfile.

docker build

Developers often use this command to create a Docker container based on their local files and folders.

Check how to build a Docker image from Dockerfile here.

docker commit

To create a new image from a container’s changes.

docker commit <container_name>

docker history

To show the history of an image.

docker history <container_name>

docker images

To list images.

docker images

docker import

To import the contents from a tarball to create a filesystem image.

docker import

docker load

To load an image from a tar archive or STDIN.

docker load

docker rmi

To remove one or more images.

docker rmi <container_name>

Tip: For more on Docker images, check our in-depth tutorials on how to update a Docker image and how to remove Docker images, including removing one image, dangling images, and how to remove all Docker images.

docker save

To save images to a tar archive or STDOUT.

docker save <container_name>

docker tag

To tag an image into a repository.

docker tag

4. Docker container commands

Managing containers is a day-to-day activity of DevOps teams. Here is a list of Docker commands to manage containers.

docker run --name

To create and run a container from an image.

docker run --name <container_name> <image_name>

docker run -p

To run a container with port mapping.

docker run -p <host_port>:<container_port> <image_name>

docker run -d

To run a container in the background.

docker run -d <image_name>

docker start|stop

To start or stop an existing container.

docker start|stop <container_name> (or <container-id>)

docker rm

To remove a stopped container.

docker rm <container_name>

docker exec -it

To open a shell inside a running container.

docker exec -it <container_name> sh

In our comprehensive tutorial, you can find more information on how to use Docker exec.

docker logs

To fetch and follow the logs of a container.

docker logs -f <container_name>

For more on Docker logs, head to our How to view Docker container logs tutorial(https://www.cherryservers.com/blog/docker-container-logs).

docker inspect

To inspect a running container.

docker inspect <container_name> (or <container_id>)

docker ps

To list currently running containers.

docker ps

docker ps -a

To list all docker containers.

docker ps -a

docker container stats

To view resource usage stats.

docker container stats

5. Docker network commands

Docker allows containers to communicate between each other. This can be done via Docker networks. Below are the Docker network commands:

docker network create

To create a new Docker network.

docker network create

docker network connect

To connect a container to a network.

docker network connect

docker network disconnect

To disconnect a container from a network.

docker network disconnect

docker network inspect

To display information about a Docker network.

docker network inspect

docker network ls

To list all the networks.

docker network ls

docker network rm

To remove one or more networks.

docker network rm

6. Docker volume commands

Docker volumes are used for permanent data storage. Containers mount those volumes and make them accessible from inside the containers.

Here are the Docker commands related to volume management.

docker volume create

To create a new Docker volume.

docker volume create

docker volume ls

To list all Docker volumes.

docker volume ls

docker volume rm

To remove one or more volumes.

docker volume rm

docker volume inspect

To display volume information.

docker volume inspect

For more information on volume management, check our detailed tutorial on how to use Docker volumes.

7. Docker Swarm commands

Docker swarm mode is an advanced Docker feature for managing a cluster of Docker daemon intended for production environments. A swarm consists of a swarm manager and nodes where services are deployed.

Docker Swarm mode supports scaling, container replicas, network overlays, encrypted communications, service discovery, and rolling updates across multiple machines.

docker node ls

To list nodes.

docker node ls

docker service create

To create a new service.

docker service create

docker service ls

To list services.

docker service ls

docker service scale

To scale services.

docker service scale

docker service rm

To remove a service from the swarm.

docker service rm

Docker Swarm mode is complex and offers many advanced Docker commands and functionalities. For more information, check our guide on how to get started with Docker Swarm container orchestration.

Conclusion

In this Docker commands cheat sheet, we've covered a non-exhaustive list of Docker commands to kickstart your journey in DevOps and Docker container management. Understanding the power of Docker and how to manage it gives you the tools needed to maintain your infrastructure. You can find more about Docker commands in the official Docker CLI reference.

Run your Docker containers at scale with Cherry Servers' open cloud infrastructure. Our dedicated bare metal and virtual servers offer hourly billing, global availability, flexible storage, and free 24/7 technical support.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: e4941077.621