Are you tired of creating virtual machines to set up a development environment? Chances are you’re looking into shifting to containerization if you haven’t done so already. And when dabbling with containerization, it is almost impossible not to come across Docker. Isn’t it time to learn docker commands.
There are many Docker products. But in this article, the Docker product that’s being referred to is the Docker Engine. Docker Engine is a containerization technology where you can build and containerize your applications.
In this article, you will learn the top useful Docker commands that will help you understand and get started on how to use them. You will learn how to download images and run containers. Even how to build a Docker image and publish to your Docker repository, and many other Docker commands.
Table of Contents
Before You Begin
Before you can start using Docker commands and follow the examples in this article, you will need to meet some requirements.
- A Docker host. This is the machine or computer where the Docker engine is installed.
- The Docker Engine must be installed on your Docker host. Note that in this article, Docker is installed on Ubuntu 18.04. However, the Docker commands should be the same in Windows or Mac.
- A Docker Hub account. This is where you can publish the Docker image that you create. If you don’t have one yet, you can sign up for a free Docker Hub account.
Getting the Docker Version Information
So you already have Docker installed. But, it is always good to know what version you’re running. Knowing what Docker version you have is essential, especially if you are concerned whether you’re still using the latest and greatest.
There are two Docker commands available for checking your Docker version;
docker --version and
docker version. They look similar, but what’s the difference?
docker version vs. docker –version
The only difference between the Docker commands
docker --version and
docker version is with how the output is displayed. For example, the screenshot below is what you’d expect to see when you run
As you can see above, the command only returned a single line of text showing the Docker version and build information. Typically, this is the only version information you would be after. But, if you want more details, then you should run the
docker version instead.
As you can see from the screenshot above, the Docker command
docker version returned more information about the Docker client and Docker server. The information includes details like version, operating system, and architecture, among others.
Which Docker command you use to get the Docker version is solely up to you. Whether one is better than the other is not the question. Instead, just use which Docker command would give you the information you need.
TIP: If you want to learn about the difference between Docker client and Docker server, visit the Docker architecture page.
Getting Help for Docker Commands
You’ve probably used other tools before, either GUI or CLI. One essential part of learning a new application is the help system. Fortunately, Docker includes a usage help system that is available with each Docker commands.
docker COMMAND –help
To get a useful reference for a Docker command, use the
docker COMMAND --help. For example, suppose you want to know what the
docker search command does and what options are available to it. In that case, you only need to append the
--help option to your command.
As you can see from the screenshot above, after running the
docker search --help command, the results showed the usage (syntax) and the available options for the docker search command.
TIP: You could also use the online Docker CLI reference if that’s what you prefer.
Managing Docker Images
The beauty of Docker is there’s plenty of pre-built images that you can choose from. Whether you’re looking for Docker-certified (official) or user/community-created images, you will most probably find an image that you need.
In this section, you’ll learn the Docker commands to:
- Search for Docker images.
- Download Docker images.
- List downloaded Docker images.
- Remove Docker images.
Docker images are downloaded from a registry, like the Docker Hub. Using the
docker pull Docker command, you can download an image or repository to your Docker host.
Suppose, for example, you are developing an application that you want to test with the Alpine distro. In that case, you could use the
docker search command to see if there’s an existing Alpine image in Docker Hub. Since you’re looking for an Alpine image, use the command
docker search alpine.
As you can see from the screenshot above, the Docker command
docker search alpine returned a list of available images matching the search term alpine. And based on the result, there’s an official image that is available (top result).
Now that you’ve determined that the Alpine image exists in the registry, you can download it using the
docker pull Docker command. To download the Alpine image, the command to use is
docker pull alpine.
The screenshot above shows that the Alpine image with the tag name latest was downloaded.
TIP: A tag is simply a way to provide version control for the image. The ‘latest’ tag typically means the latest version of the image. Other tags for the different versions of an image may exist that can be found in Docker Hub.
But, what if you do not want the latest version? Perhaps you need an earlier version of Alpine? For that, you will have to go to Docker Hub using your web browser to look for the available Alpine image tags.
As you can see from the Docker Hub search demo above, there are other versions of the Alpine image. If you want to download the image with the specific tag, you need to modify your command and append the
:tagname to the image.
For example, to download the alpine image with tag 3.10, use this Docker command –
docker pull alpine:3.10.
As you can see from the result above, the image alpine 3.10 was downloaded.
So far, two Docker images were downloaded using the
docker pull Docker command. Did downloading a different version of the Alpine image remove the other version(s)? How do you know what images are already downloaded into your Docker host?
Pulling other versions of an image does not automatically remove the versions previously downloaded. To know which docker images currently exist on your Docker host, use the
docker images Docker command.
Now that you know which Docker images are on your Docker host, you can decide which ones you still need and which ones you want to remove. Naturally, deleting an image that you no longer need translates to freeing up disk space.
For example, if you want to delete the latest alpine image, you can use the command
docker rmi [image id]. In the example below,
[image id] is
You could also use the
docker rmi [name:tag] command. In the example below, the image to be removed is
Managing Docker Containers
Now you know how to get images, it is time for you to learn the common Docker commands to put those images in use. In this section, you’ll learn the Docker commands to:
- Create a new Docker container from a Docker image.
- List existing Docker containers.
- Start, stop and kill Docker containers.
- Execute commands from the running Docker containers.
- Rename existing Docker containers.
So you already have an image downloaded to your Docker host. Your next step is to use that image for the purpose you intended. In this example, an Ubuntu image is downloaded as you can see from the screenshot below.
To use an image, you will have to create a Docker container based on that image. To do that, use the Docker command
docker run. For example, to create a new container based on the Ubuntu image and run it in the background, use this the command –
docker run -itd ubuntu.
What did the
-itd option do? Here’s the breakdown according to the
docker run help definition.
i– This option means that the container will run interactively.
t– This option will create a pseudo-TTY (terminal) where you can enter your commands.
d– This option runs the container in the background and will keep on running until it is stopped or killed.
docker ps command is what you need to list all existing containers in your Docker host. Using
docker ps without options will list only the running containers.
If you want to list all existing containers, including the ones that are not running, use the Docker command
docker ps -a.
As you can see from the screenshot above, the status of the container with container ID
Exited, which means that the container is not running.
Whenever you use the command
docker run, a new container is created. And, if you do not use the option
--name, a random name will be assigned to the container you are creating. As you can see in the image below, the container created from the ubuntu image was assigned the name heuristic_curie.
You can already tell that that name doesn’t mean anything. You may want to change the container’s name to something that you can easily recognize. For example, if you plan to use the container for developing a website hosted in Apache, perhaps you can change the name to ubuntu_apache_dev.
To rename a container, you can use the
docker rename command. In the example below, the container is renamed from heuristic_curie to ubuntu_apache_dev using this code –
docker rename heuristic_curie ubuntu_apache_dev.
docker stop vs. docker kill
When you’re done using a container, and you want to stop it, there are two Docker commands you can use;
docker stop and
docker kill. But what’s the difference?
docker stop [CONTAINER] will give the container time to perform a graceful exit. By default, the
docker stop command will wait 10 seconds before putting the container into a halt. If you think that the container needs to be given more time, you can add the
-t option to the command.
For example, to instruct
docker stop to wait 20 seconds before completely stopping the container, use the command
docker stop [CONTAINER] -t 20.
On the other hand, the command
docker kill will terminate the container immediately. There are standard Linux signals that you can use with the
-s option. For example, to kill a container with a termination signal, the entire command would be
docker kill -s=SIGTERM [CONTAINER].
So you’ve stopped the container. But, what if you need to use the container again? You need to run the
docker start command. The
docker start is used to start a currently stopped container.
If you want to start a container in the background, issue the command
docker start [CONTAINER]. In the screenshot below, the container ubuntu_apache_dev is started.
Next, if you want to start a container and interact with it, you can start the container in interactive mode. To start a container in interactive mode, use the command
docker start -i [CONTAINER]. In the screenshot below, the container ubuntu_apache_dev is started in interactive mode.
docker exec command is used to execute commands in running Docker containers.
In the previous example, there is a running container based on the ubuntu image with the name ubuntu_apache_dev. Suppose you want to update the Ubuntu repository inside that container. In that case, you can issue the
apt update command with
The example below shows how to run
apt update command in the ubuntu_apache_dev using the command
docker exec ubuntu_apache_dev apt update.
As you can see from the screenshot above,
apt update was executed in the container without having to enter the container. But, if you want to enter the running container’s bash console, you can use the command
docker exec -it [CONTAINER] bash.
As you can see, the command opened a bash prompt that is running in the container. Now you can use bash and issue commands that will be run in the container.
Building and Publishing New Docker Images
After you’ve made changes to a container, like installing applications or adding users, you can commit them to a new docker image. The resulting Docker image can then be shared and published to your docker hub repository.
In this section, you’ll learn the Docker commands to:
- Commit the changes made in a Docker container to a new Docker image.
- Sign in to your Docker Hub account.
- Tag and Push Docker images to Docker Hub
- Build a new Docker image using a Dockerfile
Suppose you created a container based on the ubuntu docker image and installed a software in it like nano. That means you already created a change in the container. To commit the changes made to a container into an image, the Docker command to use is
Using the command
docker exec ubuntu_apache_dev apt -y install nano, nano will be installed in the ubuntu_apache_dev container.
Then, using the command below, the container ubuntu_apache_dev will be committed to a new docker image with the name ubuntu_apache_dev-nano.
docker commit -a "june castillote" -m "installed nano" ubuntu_apache_dev ubuntu_apache_dev-nano
-a option adds the author’s information to the commit. While the
-m option adds a message to the current commit.
After the commit has been completed, check to see that a new image has been created by running
docker images. As you can see from the screenshot below, a new image with the name junecastillote/ubuntu_docker_dev and with tag nano is listed.
You could repeat the same process whenever you need to create a new docker image from
Now that a new image has been created using
docker commit, the resulting image can be published to your docker hub repository. However, before you can publish the image, you must authenticate to docker hub first. For that, you’ll need to use
The command below will authenticate with docker hub using the user junecastillote.
docker login -u junecastillote
When you run the command above, you would be prompted to enter the password. Enter the password and wait for the authentication to complete. You should see a similar output, as shown below.
docker tag and docker push
To publish or push a new image to the Docker hub registry, you will need to use two Docker commands;
docker tag and
First, create a target image using the command
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]. In the example below, the source image is the image that was created in the previous section, which is
ubunt_apache_dev-nano. The target image is
# Create a new tagged image docker tag ubuntu_apache_dev-nano junecastillote/ubuntu_apache_dev:version-0.1 # List images docker images
When you run the command above, you should see a similar result, as shown below.
To publish or push the image to the registry, use the Docker command
docker push NAME[:TAG]. In the example below, the image junecastillote/ubuntu_apache_dev:version-0.1 will be pushed to a new docker hub repository.
docker push junecastillote/ubuntu_apache_dev:version-0.1
When you run the command above, you should see a result similar to the image below.
As you can see in the screenshot above, after pushing the image to docker hub, a new repository has been created.
The last Docker command you’ll learn in this article is
docker build. The
docker build command, as the name implies, is used to build an image from the context or instructions found inside the
For example, if you want to build a new image that consists of:
- Using the alpine image as the base.
First, you need to create an empty file named Dockerfile. The quickest way to create it is by running this command –
Next, open the Dockerfile using your text editor such as
nano if you’re on Linux. Once the Dockerfile is open for editing, add these lines below.
FROM alpine RUN apk update && apk add \\ nano \\ sudo
The code above will use the alpine image as the base and run the installation of
sudo. The image below shows the Dockerfile being edited using
Now that the Dockerfile is ready, it can be used to build the new docker image using the command
docker build -t [NAME:TAG] [PATH to Dockerfile]. In the example below, the image will be built with the name
junecastillote/alpinex:1.0 using the Dockerfile located in the current directory.
docker build -t junecastillote/alpinex:1.0 .
The command above will result in the output that you can see below.
As you can see from the result above, the build process happens in steps or “layers”. In the example above, two steps happened.
- The first step was to pull the alpine image from the Docker registry.
- The second step was to update the package manager, install nano and sudo.
docker build command completed the steps outlined in the Dockerfile, you should see that a new Docker image has been created.
Docker is an excellent solution for building and running containerized applications. Just like any other software, you need to learn how what the Docker commands are, understand, and understand how to use them.
In this article, you’ve learned the top useful Docker commands that can help you start with using the Docker CLI. You’ve learned the Docker commands to perform the following actions:
- Finding out the Docker version installed on a Docker host.
- Searching and downloading Docker images from the Docker registry.
- Listing and deleting Docker images available on the Docker host.
- Creating, listing, starting, stopping, and removing Docker containers.
- Commit changes to a Docker container into a new Docker image.
- Tagging and pushing a Docker image to the Docker registry/repository.
- Building a new Docker image using a Dockerfile.
The knowledge you learned in this article only covered the basics of using Docker commands. I hope that learning these commands can get you started on your Docker journey. Perhaps, if you’re automating Docker tasks, you can use these Docker commands in scripting as well.
Thank you for reading!