20 Essential Docker Commands You Need to Know

In this article, I will introduce you to the world of Docker and its essential commands. Docker is a platform that helps developers build and deploy containerized applications, and it solves compatibility challenges by providing lightweight virtualized environments. The primary command line interface (CLI) of Docker supports nearly 60 subcommands that can tackle different tasks. Whether you’re using Docker Engine or Docker Desktop, familiarizing yourself with these 20 essential Docker commands will be beneficial. So let’s dive in and discover which commands you should know!

20 Essential Docker Commands You Need to Know

Getting Your Hands on the Docker CLI

Your Docker environment might be the open-source Docker Engine or the user-friendly GUI interface of Docker Desktop. The CLI will be your primary interface in a Docker Engine environment, but you’ll also gain access to the command line tool if you install Docker Desktop.

We think that even users of Docker Desktop will find themselves sliding into the CLI once they become familiar with at least these 20 essential docker commands.

20 Essential Docker Commands You Need to Know

Docker Commands You Should Know

Let’s dig in and find out which Docker commands you should be most familiar with.

1. docker system

The docker system command helps inspect and manage the Docker environment. It supports command-line arguments for performing specific management-related tasks, including:

  • df: shows how you are using the disk
  • prune: removes unused networks, containers, images, or volumes
  • info: shows system-related information
  • events: displays a log of system events in real-time

Below is the initial prompt after executing docker system prune.

docker system prune output

2. docker context

This command helps you navigate and configure different contexts. In Docker, contexts include names, transport layer security info, endpoint configurations, and orchestrators.

Some of the docker context arguments:

  • ls: displays the details of the default context
  • inspect [CONTEXT]: inspects a specified context
  • create [CONTEXT]: creates a new context
  • use [CONTEXT]: switches between contexts

Below is an example of output from docker context ls.

docker context ls output

3. docker pause and unpause

The docker pause command freezes a container’s active processes. To run it, you must specify the container’s name.

Below is an example of the output you can expect after pausing a container.

docker pause output

The docker unpause command resumes paused container processes. Like the previous command, you must specify the container name.

Below is an example of the output you can expect after unpausing a container.

docker unpause output

4. docker rm

This command removes containers, volumes, and networks. It allows selecting the component to remove based on its attributes. For instance, you can force-remove running containers or all specified containers.

docker rm [CONTAINER] removes the container whose name is specified. The output of this command is in the screen capture below.

docker rm output

5. docker rmi

You use this command to remove images. You can remove a single image or multiple images at once. You can describe the image to remove using either the short ID or the long ID. The command is important for keeping the host node clean and efficient.

The command for image removal uses this structure:

docker rmi [IMAGE ID]

Below is an example of its output.

docker rmi output

6. docker volume

This command allows you to manage volumes in Docker. You can use it to create, remove, list, and inspect volumes.

Some of the docker volume arguments are:

  • create [OPTIONAL NAME]: creates a new volume. You can specify the volume name or let Docker generate a random name.
  • ls: lists the volumes available
  • inspect [NAME]: shows detailed volume information.
  • rm [NAME]: removes a volume from Docker.

Below is an example of the output after creating a volume.

docker volume create output

7. docker search

Use this command to search for images from Docker Hub, which you can then run as containers on your machine. It allows you to access Docker Hub registry images without visiting the website.

The command follows this structure: docker search. You can specify the names of the images you are searching for or create a filter.

Below is an example of the output from the following query:

docker search --filter is-official=true --filter stars=500 mysql

docker search output

8. docker push

The docker push command allows you to share your images in the Docker Hub registry or a private repository.

The structure of the command is:

docker push [OPTIONS] NAME[:TAG]

[OPTIONS] allows you to set -disable-content-trust.

By default, this value is true, and it’s not mandatory to include it.

NAME[:TAG] requires using the registry’s name, the repository, and the image tag.

Below is an example of the output from docker push.

docker push output

9. docker pull

This command downloads a Docker image from a repository in a private or public registry.

The command works like this:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

This command allows you to use existing images instead of creating new ones whenever you must create a containerized application.

The example below shows the output from a docker pull command:

docker pull output

10. docker ps

By default, this command displays the list of all the running containers. However, you can add a flag to list containers based on attributes such as disk usage size, linked containers, and labels.

The command follows the structure below:

docker ps [OPTIONS]

Some of its arguments include:

  • -a: displays a list of running and exited containers
  • -s: displays each container’s on-disk size and virtual size

You can use the two together like this:

docker ps -as

Below is an example of the output from a docker ps command.

docker ps output

11. docker tag

Use this tag to add metadata, such as the version, to your image. Tags are typically created when an image is built, but the docker tag command allows you to add a tag later, essentially creating an alias for the target image.

This command follows the following structure:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

In the example below, we list images with the name “redis.” After tagging the image with a version number (“2.0”), the new alias and the originally tagged image appear in the list.

docker tag output

12. docker rename

Use this command to rename a container. This is useful when you have several containers and want to differentiate them based on their purpose.

This command follows the following structure:

docker rename [OLD_NAME] [NEW_NAME]

Below is an example of output before and after a docker rename command.

docker rename output

13. docker commit

This command allows you to create new images after changes to a container’s files. This is important because it enables you to debug a container using an interactive shell.

This command follows the following structure:

docker commit [CONTAINER_ID] [name-of-new-image]

Below is an example and output from the docker commit command.

docker commit output

14. docker network

This is a network management command that allows you to create powerful applications by connecting services and containers.

The command has the following structure:

docker network [OPTIONS]

Its arguments include:

  • connect: for connecting containers to networks
  • create: for building new networks
  • disconnect: for disconnecting running containers from networks
  • rm: for removing single or multiple networks

Below is the output from a docker network create command.

docker network create output

15. docker history

This command gives the history of a specified image, helping you understand how it was created and displaying the image size.

The command has the following structure:

docker history [IMAGE]

Below, we see the history associated with the image redis:latest.

docker history output

16. docker update

This command enables you to update a container’s configuration. It helps prevent containers from consuming too many Docker host resources. The format of the command is:

docker update [OPTIONS] [CONTAINER]

Some of its options include:

  • –restart: updates a container restart policy
  • –memory: sets a container’s memory limit
  • –cpus: sets the number of allocated CPUs

Below is an example of output from a docker update command.

docker update output

17. docker plugin install

This command allows you to manage plugins. It is essential because it allows adding new functionalities without altering Docker host configurations.

The docker plugin arguments include:

  • create: for creating new plugins
  • enable: for enabling installed plugins
  • install: for installing new plugins
  • rm: for removing single or multiple plugins
  • ls: for displaying a list of plugins

Below, we use docker plugin install to add a plugin to our environment. Then, we use docker plugin ls to reveal its status.

docker plugin ls output

These are just a few of the many powerful commands available in the Docker CLI. By familiarizing yourself with these commands, you’ll be able to manage and work with containers more efficiently.