Top 25 Essential Docker Commands You Need to Know

Top 25 Essential Docker Commands You Need to Know

·

10 min read

Introduction

In my previous article, we talked about git and the 15 commands you need to know to master it. In today’s guide, we will explore 25 essential Docker commands that will help you master Docker and improve your container management skills. Whether you're new to Docker or looking to enhance your knowledge, this article will equip you with the necessary commands to leverage Docker effectively.

What is Docker?

Before diving into the essential Docker commands, let's briefly understand what Docker is. Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications using containerization. Containers are lightweight, isolated environments that encapsulate an application and its dependencies, making it easier to deploy and run consistently across different environments. Docker provides a seamless way to package and distribute applications as self-contained units, ensuring consistency and reproducibility.

Docker Version

Before starting to use Docker, it's important to check the version of Docker installed on your system. This allows you to verify if your Docker installation is up-to-date and compatible. The docker version command provides detailed information about the Docker client and server versions, along with the API version used.

$ docker version

One of the key advantages of Docker is the availability of a vast repository of pre-built images. The docker search command allows you to explore the Docker Hub registry and find images relevant to your project. By specifying a search term, you can discover images shared by the Docker community.

$ docker search <search_term>

For example, to search for Ubuntu-based images, you can use the following command:

$ docker search ubuntu

The command will display a list of relevant images, along with their descriptions, star ratings, and other details, enabling you to choose the most suitable image for your requirements.

Docker Pull

Once you've identified the desired Docker image, you can use the docker pull command to download it from the Docker Hub registry. This command fetches the specified image and its associated layers to your local machine, making it available for use in your containerization workflow.

For instance, to pull the latest Ubuntu image, you can execute:

$ docker pull ubuntu:latest

Docker will download the image and provide a progress status, allowing you to track the download process.

Docker Run

After pulling an image, you can create and run a container using the docker run command. This command not only creates an instance of the specified image but also starts its execution.

To run a basic Ubuntu container, you can utilize the following command:

$ docker run -i ubuntu

This will create a new container and provide you with a shell prompt inside the container, enabling you to interact with it.

A container is an instance of a Docker image that is running.

When the container is running it will block your terminal, to avoid that use the -d (--detach) option with docker. Here is an example:

$ docker run -d nginx

Docker PS

To monitor the running containers on your Docker host, you can use the docker ps command. It displays a list of active containers, providing essential information such as container IDs, names, image names, and status.

$ docker ps

This command will show a concise list of running containers, allowing you to keep track of the containers that are currently active.

If you want to see both running and stopped containers use the --all option

$ docker ps --all

Executing this command will provide you with a comprehensive list of all containers on your Docker host, regardless of their current state.

Docker Stop

When you want to stop a running container gracefully, the docker stop command comes in handy. It sends a termination signal to the specified container, allowing it to perform a clean shutdown.

$ docker stop <container_id>

For example, to stop a container with the ID "b6a46b6c9206" you can run the following command:

$ docker stop b6a46b6c9206

Instead of specifying the container ID as an argument to the docker stop command you can also provide the container name.

Docker Restart

In some cases, you may need to restart a stopped container. The docker restart command allows you to restart a container that is in a stopped state.

To restart a container with the ID "b6a46b6c9206" you can use the following command:

$ docker restart b6a46b6c9206

The command will start the specified container, allowing it to resume its execution. Instead of the container ID, this command also takes the docker container name as an argument.

Docker Kill

If you need to forcefully terminate a running container, you can utilize the docker kill command. This command sends a SIGKILL signal to the specified container, causing an immediate termination.

To forcefully terminate a container with the ID "b6a46b6c9206" you can execute:

$ docker kill b6a46b6c9206

Keep in mind that using docker kill is equivalent to abruptly cutting off power to a running machine. Because it is an abrupt way to force termination in typical emergency situations, it is always recommended to use docker stop first.

Docker Exec

To run a command within a running container, the docker exec command is your go-to option. This command allows you to execute a command in a running container's environment.

$ docker exec <container_id/name> <command>

For example, to execute a shell command within a container with the ID "07fb535f009a" you can use the following command:

$ docker exec 07fb535f009a ls

This will run the ls command inside the specified container, displaying the contents of the current directory.

Docker Login

When working with private Docker registries or repositories, you'll need to authenticate yourself using the docker login command. This command prompts you to enter your username and password to securely access the private resources.

$ docker login

Docker Commit

Once you've made changes to a container and want to create a new image based on those changes, you can utilize the docker commit command. This command captures the current state of a container as a new image.

$ docker commit <container_id> <new_image_name>

For instance, to create a new image named "myapp:latest" from a container with the ID 9da89f3673cd" you can execute:

$ docker commit 9da89f3673cd myapp:latest

This will create a new image with the specified name and tag, preserving the changes made in the container.

Docker Push

To share your Docker images with others or deploy them to remote servers, you can use the docker push command. This command uploads your local image to a Docker registry or repository.

$ docker push <image_name>

For example, to push an image named "myapp" to a Docker registry, you can execute:

$ docker push myapp

This will make your image available for others to pull and use in their own environments.

Docker Network

Docker allows you to create and manage networks to facilitate communication between containers. The docker network command helps you in creating, inspecting, and managing networks.

For instance, to create a new bridge network named "demo-network" you can use the following command:

$ docker network create demo-network

This command will create a bridge network that containers can be connected to, enabling seamless communication between them.

Docker History

To gain insights into the layers that make up an image, the docker history command is invaluable. This command provides a chronological view of the changes and commands executed to build an image.

For example, to view the history of an image named "myapp:latest," you can execute:

$ docker history myapp:latest

This command will display a list of layers, their sizes, and the commands executed at each stage of the image creation process.

Docker RMI

When you no longer need a specific Docker image, you can remove it from your local machine using the docker rmi command. This command deletes the specified image from your local image cache.

To remove an image named "myapp:latest," you can run the following command:

$ docker rmi myapp:latest

Docker rm

Docker uses a similar syntax to remove containers as it does to remove files and directories via the Linux terminal.

$ docker rm container_id_or_name

Note: A running container cannot be removed. You cannot delete an image that is associated with a container, even if it is stopped.

Docker Copy

Sometimes, you may need to copy files between your local machine and a running container. The docker cp command allows you to accomplish this task.

$ docker cp <source_path> <container_id>:<destination_path>

For example, to copy a file named "data.txt" from your local machine to a container with the ID "d1b4d6b06923" in the directory "/" you can execute:

$ docker cp data.txt d1b4d6b06923:/

Docker Logs

Monitoring and troubleshooting containerized applications is crucial. The docker logs command enables you to view the logs generated by a specific container, aiding in debugging and analysis.

To view the logs of a container with the ID "d1b4d6b06923" you can use the following command:

$ docker logs d1b4d6b06923

This command will display the logs generated by the container, allowing you to identify any issues or errors.

Docker Volume

Docker volumes are a convenient way to persist data beyond the lifespan of a container. The docker volume command helps you manage volumes and their lifecycle.

For instance, to create a new volume named "demo-volume," you can use the following command:

$ docker volume create demo-volume

This command will create a volume that can be mounted to containers to store and retrieve data.

Docker Logout

When you're finished working with a private Docker registry, it's important to log out to ensure the security of your credentials. The docker logout command allows you to securely log out from the Docker registry.

$ docker logout

By executing this command, you will be logged out from the currently authenticated Docker registry.

Docker top

The top command is a Linux command that you may be familiar with. It is used to display a list of currently running processes on the system. Docker top displays a list of the processes that are currently running within a container.

$ docker top container-name-or-id

Docker stats

This docker stats command can be used to display a live stream of container(s) resource usage statistics or a specific container system resource usage.

$ docker stats

Docker diff

You can inspect changes to files or directories on a container's file system using docker diff. It displays the files and directories that have changed in a container's filesystem since it was created.

There are three types of change tracked (A/D/C):

  • A: Creation of a new file or directory

  • D: File or directory deletion

  • C: Modification of a file or directory

Let's do to our container with this id "d1b4d6b06923":

$ docker diff d1b4d6b06923

Docker rename

This command is used to rename a docker container:

$ docker rename nginx-server myserver

Note that you can do this even when the container is up and running!

Docker pause and docker unpause

Docker pause allows you to pause all processes in a specific container. The SIGSTOP signal is used, which is detected by the suspended process.

$ docker pause nginx-server

You can now use the docker ps command to see if it was truly paused:

To resume it, simply use unpause:

$ docker unpause nginx-server

Conclusion

Congratulations! You have now familiarized yourself with 20 essential Docker commands crucial for mastering Docker and efficiently managing your containerized applications. From pulling and running containers to managing networks, volumes, and images, these commands provide you with the necessary tools to streamline your containerization workflows. By incorporating these commands into your DevOps practices, you'll enhance your productivity and gain greater control over your application deployments. So go ahead, experiment, and leverage the power of Docker to unlock new possibilities in your software development journey.

Remember, Docker offers a vast array of commands and functionalities beyond what we covered in this beginner's guide. Explore the official Docker documentation and additional resources to expand your knowledge and become a proficient Docker user.

For more insightful articles like this check us on our website.

Did you find this article valuable?

Support sysxplore by becoming a sponsor. Any amount is appreciated!