Enroll in Selenium Training

Docker offers a set of "platform-as-a-service" products that help us develop and deploy applications by packaging them into containers. Moreover, for the development and deployment of these applications, Docker provides a new range of terminology and a new set of commands. So far, we have gotten acquainted with major concepts in Docker like Docker Containers, Docker Image, Dockerfile, etc. Additionally, we have also seen a few Docker commands while exploring these topics in detail. But, it is always beneficial to have a reference point for all the common commands. Subsequently, in this tutorial, you will find the most common commands in one place. The article will cover:

  • Docker Container Commands
  • Docker Image Commands
  • Also, Docker Commands for information
  • Docker Network Commands

Docker Container Commands

The Docker Containers are lightweight and portable components and are virtual environments that we can share without having to risk inconsistencies in development. Additionally, the table below summarises various Docker container commands:

Container Commands Usage
Create a Container docker create [IMAGE]
Rename a container docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]
Run container docker run [OPTIONS] [IMAGE] [COMMAND] [ARG...]
Remove or delete the container docker rm [OPTIONS] [CONTAINER]
Start a container docker start [OPTIONS] [CONTAINER]
Stop a container docker stop [OPTIONS] [CONTAINER]
Restart a container docker restart [OPTIONS] [CONTAINER]
Pause container docker pause [CONTAINER]
Unpause container docker unpause [CONTAINER]
Update container docker update [OPTIONS] [CONTAINER]
Attach local standard I/O to a container docker attach [OPTIONS] [CONTAINER]
Block container till others stop docker wait [CONTAINER]
Kill a container docker kill [OPTIONS] [CONTAINER]

Some of the commands contain a few options that can apply to the command. For example, in the docker restart command, we can specify the time to consider before restarting as part of the options. Moreover, you can explore these options on Docker's official website.

Consequently, let us understand the syntax and usage of all these commands in more detail in the following sections:

docker create command

We use/ run the "docker create" command to create a container. Moreover, this command creates a writeable container layer over the given Docker image.  Note that the container created due to the 'docker create' command is not running. Moreover, we can start it using the 'docker run' command. Subsequently, the general syntax of the command is:

docker create [IMAGE]

Here [IMAGE] is the image for which a container layer is to be created. Consequently, on successful execution, the command returns container Id STDOUT.

Let us create a container around the image named 'ubuntu'. Moreover, the output of the command is: 1-Docker command - create container.png

We can see that the command returns the ID of the newly created container. Next, run the command 'docker ps'. Consequently, we see the output is empty. This is because the container just created is not running. Additionally, if we provide the flag -a with the 'docker ps' command, we will see the container we just created (shown in the above output). The "docker create" command is beneficial when creating a container for future use and not for the time being. Moreover, you want the container to be ready when it needs to get started.

docker rename command

The command to rename an existing container is "docker rename". This command takes the container's current name and the new name as an argument and renames the container with the new name. Additionally, the command has the following syntax:


Let us rename a container now using the following command:

docker rename happy_lewin myUbuntuContainer

Moreover, the output of the above command is shown below. 2-rename a Docker container.png

The above output shows renaming a container named 'happy_lewin' to myUbuntuContainer below the first highlighted box.

docker run command

The 'docker run' command creates a writeable container layer over the given image and then starts it using the specified command as a parameter. Additionally, the command is equivalent to "docker create" + "docker start". The difference being that the "docker run" also starts the container along with creating it. Moreover, this command has the following syntax.

docker run [IMAGE] [COMMAND]

The 'docker run' command has the following variations.

  • docker run --rm [IMAGE]– removes/deletes the container once it exits.
  • docker run -td [IMAGE]– start a container and keep it running state.
  • Next is, docker run -it [IMAGE] - start a container and allocates a pseudo-TTY connected to the container’s stdin. Additionally, it also creates an interactive bash shell in the container.
  • docker run -it -rm [IMAGE] – create, start, and run a command inside the container. Moreover, it removes the container once the command is executed.

The following screenshot shows the execution output of this command. 3-Create and run container.png

Here we have created and started a container named ubuntu. Moreover, when we execute the 'docker ps' command, we can see this newly created container listed in the output.

docker rm command

When the container is not running, we can remove it using the 'docker rm' command. Moreover, the usage for this command is as follows:

docker rm [CONTAINER]

We simply pass the container ID as an argument to the docker rm command.

Following screenshot shown the working of the 'docker rm' command. 4-remove container.png

Here we have a running container shown as output of the first command, docker ps. Note that if we try to remove it, Docker will give an error since the container is running. Therefore, we need to stop the container first before using the 'docker rm' command to remove the container. The above screenshot shows this. So first, we stop the running container and then remove it. Additionally, we have verified this by listing the container. Consequently, we see the container does not exist now.

docker start command

We can start one or more containers that are created using the 'docker create' command or are stopped earlier using the 'docker start'. Additionally, the general syntax for the command is as given below.

docker start [CONTAINER]

Let us now create a new container using the 'docker create' command and then start it using 'docker start' commands. 5-Docker command - start a container.png

We see that as a result of the execution of the 'docker create' command, a new container is created (check the container status). After that, we start the container with the "docker start" command. Consequently, successful execution returns the container id or name. Now, if we check the status, it shows the container has exited. In other words, this means it was started and then exited.

docker stop command

As we can start a stopped container, we can also stop a running container using the command 'docker stop'. As a result of this command, the main process inside the container receives SIGTERM, and after a stipulated grade period, it receives SIGKILL that will stop the container. Additionally, given below is the general usage of the stop command.

docker stop [CONTAINER]

The command 'docker stop' can be provided with a --time (or -t) option to specify the time limit in terms of seconds before the container can be stopped and killing it. For example, say we want to wait for 500 seconds to stop the container. Consequently, we can provide the command as follows:

docker stop --time 500 xenodochial_cohen

Here, 'xenodochial_cohen' is the name of the container to be stopped. So here we will wait for 500 seconds to stop before killing this container.

docker restart command

We use the 'docker restart' command to restart the container, i.e., stop the running container and start it again. The general syntax of the restart command is:

docker restart [CONTAINER]

We can restart the container we stopped above using this command. 6-restart a container.png

The above screenshot shows the starting of the stopped container.

docker pause command

The 'docker pause' command pauses or stops a container temporarily by suspending all the processes in the 1given container. Following is the usage of this command.

docker pause [CONTAINER]

docker unpause command

The 'docker unpause' command is the opposite of the 'docker pause' command. This command un-pauses all the processes in the given container. The usage of this command is:

docker unpause [CONTAINER]

The screenshot below shows the pausing and unpausing of the container. 7-pause/unpause a Docker container.png

The above screenshot shows the pause/unpause of the container. First, we pause the running container and then unpause it.

docker update command

We can even update the container configuration dynamically. This is performed using the 'docker update' command. Using this command, we can prevent containers from overconsuming the resources from the Docker host. The 'docker update' command can update one or many containers at a time. The general syntax of the command is:

docker update [CONTAINER]

docker attach command

A standard input/output or error terminal can be attached to the running container in Docker. This is done using the 'docker attach' command. By attaching a running container to these I/O streams, we can view the container's ongoing activities and control them interactively. For example, this command has the following syntax:

docker attach [CONTAINER]

We use the 'docker attach' command to attach a standard terminal to a container named 'zealous_turing'. The screenshot is shown below. 8-docker attach command.png

We can see the terminal is opened when we attach the container to standard I/O. Note that you can also attach to the same contained process multiple times simultaneously, from different sessions on the Docker host.

docker wait command

The command 'docker wait' blocks the container until one or more containers stop. Then it prints their exit codes. In other words, this command waits on other containers. The usage of this command is:

docker wait [CONTAINER]

So if we have a system where we depend on other containers to proceed further or such, we can block the current container until the other stops.

docker kill command

The command 'docker kill' kills one or more containers. The main process inside the container sends the SIGKILL signal or the signal specified with the --signal option on the execution of this command. We can specify the container to be killed using the container ID or name or ID-prefix.

docker kill [CONTAINER]

Let us execute the kill command. Consider the following screenshot. 9-docker kill command.png

In the above screenshot, we see a running container, 'xenodochial_cohen'. We execute the 'docker kill' command, and then if we check the status, we see the container has exited with code 137.

Docker Image Commands

The below table shows important commands that work with Docker Images.

Docker Image Commands Usage
Create a Docker Image from Dockerfile docker build [OPTIONS] [URL]
Pull an Image from the registry docker pull [OPTIONS] [IMAGE]
Push an image to a registry docker push [OPTIONS] [IMAGE]
Create an image from Container docker commit [OPTIONS] [CONTAINER] [NEW_IMAGE_NAME]
Remove an image docker rmi [OPTIONS] [IMAGE]
Save an image docker save [OPTIONS] [IMAGE] > [TAR_FILE]

Let us now discuss each of the above commands.

docker build command

We can build an image from a Dockerfile in the current directory using the 'docker build' command. The docker build command creates/builds a Docker image using a Dockerfile and a “context”. A  context is the set of files located in the specified PATH or URL.

docker build [URL]

A variation of this command, docker build -t builds an image from a Dockerfile in the current directory and tags the image.

Consider the following Dockerfile.

FROM ubuntu


RUN apt-get update

CMD ["echo", "Hello World"]

Now, let us give the following command to build a Docker image from this Dockerfile.

docker build -t my_docker_image .

An image named 'my_docker_image' is created on the execution of the above command, as shown in the below screenshot. 10-Docker command - build a docker image from Dockerfile.png

We can verify the creation of this new image using the 'docker images' command, and this image will be listed as shown in the below screenshot. 11-image list.png

Apart from specifying a Dockerfile to build a new image, we can also build a Docker image with the same command using URL, for example, directly from the GIT repository. or to a remote tarball. Refer to docker build for more details.

docker pull command

Instead of creating a brand new image, we can also pull an existing image from a container registry like the Docker Hub that contains many pre-built images. The command to pull an image is as follows:

docker pull [IMAGE]

If we want to pull an image named 'Redis' from the hub, we will give the command and get an output shown in the below screenshot. 12-Docker command - pull image from registry.png

We can see the entire process that is carried out when pulling the Redis image from the hub.

docker push command

We pull images from the registry onto our Docker host. Similarly, we can push an image or a repository to a container registry. For this purpose, we use the 'docker image push' command, whose general syntax is given below.

docker push [IMAGE]

docker commit command

We can also create a new image from an existing container by container's changes through the docker commit command. Here we usually commit the container changes or setting to a new image. One advantage of creating an image from a container is to debug a container by running an interactive shell. We can also export the working dataset to a different server. The usage of this command is as follows:

docker commit [CONTAINER] [NEW_IMAGE_NAME]

For example, consider the following screenshot. 13-Docker command - create Docker image from container.png

Here we have a running container (shown by docker ps output). We use this container ID in the docker commit command to create a new image by the name 'my_docker_image'. We see the command returns an ID for the image just created.

docker rmi command

The Docker also allows removing or untagging of one or more images from the host node. To do this, we use the command "docker rmi". The general/ common syntax of this command is:

docker rmi [IMAGE]

The above command does not remove the docker image from a registry. To remove a docker image of a running container, we have to specify the -f option.

Note that if an image has multiple tags, this command will remove the tag only. If the tag is for only one image, then both the image and the tag are removed/ deleted.

We can simply give the command 'docker rmi my_container_image' to untag the image.

docker save command

We can use the 'docker save' command to save one or more images to a tar archive. This produces a tarred repository to the STDOUT. The repository contains all parent layers and all tags and versions or specified repo::tag. The general/ common syntax of this command is:

docker save [IMAGE] > [TAR_FILE]

Let us save an image named 'testimage' to a tar file, as shown in the below screenshot. 14-save Docker image.png

The command 'docker save testimage >testimage.tar' saves the tar archive file locally. When we execute the 'ls' command, it lists the archive file, as seen from the above screenshot.

Docker Commands for Information

Apart from creating and maintaining images and containers, Docker also provides various commands that help us retrieve important information about containers, images, and other Docker objects. The following table contains commands that will provide details on images and containers.

Docker Information Commands Usage
Get Docker version docker –-version
List running containers docker ps
List logs from a running container docker logs [CONTAINER]
List low-level information about Docker Objects docker inspect [OBJECT_NAME/ID]
List real-time container events docker events [CONTAINER]
Show container mapping (port or any specific) docker port [CONTAINER]
Show running processes in a container docker top [CONTAINER]
Show live usage for container statistics docker stats [CONTAINER]
Show changes to files docker diff [CONTAINER]
List all images docker image ls
Show image history docker history [IMAGE]

Let's understand the details of all these commands in the following sections:

docker version command

The command 'docker --version' provides information about the version of the Docker present on the host. Additionally, the command syntax is

docker --version

When executed, this command gives the following output. 15-Get Docker version.png

Additionally, the output shows the version and the build number of Docker.

docker ps command

We can list the containers present on the host using the 'docker ps' command. Additionally, the syntax of the command is as follows:

docker ps

To get the list of running containers and those that have stopped, we can provide a '-a' option for the above command.

docker ps -a

The following screenshot shows the output of the 'docker ps' command. 16-List containers.png

Here we can see the details of the running container.

docker logs command

We can fetch the logs of a Docker container using the 'docker logs' command. This will get the logs present at the time of its execution. Additionally, the general syntax of the command is:

docker logs [CONTAINER]

docker inspect command

If we need any low-level information on any of the Docker objects,  we can use the command 'docker inspect'. This command gives us detailed information on the constructs controlled by Docker. Moreover, the usage of the command is:

docker inspect [OBJECT_NAME/ID]

The command 'docker inspect' renders the output in JSON array by default.

For example, consider the following command,

docker inspect cbfa678479b6

Here 'cbfa678479b6' is a container ID. Moreover, the output of this command is shown below. 17-Docker inspect.png

Thus 'docker inspect' command gives all information about the Docker object we have specified as an argument.

docker events command

The command 'docker events' gets the real-time events from the server per Docker object type. Moreover, different event types will have different scopes like local scoped, swarm scoped, etc.

docker events 

docker port command

The command 'docker port' lists port mappings or a specific mapping for the container. Additionally, its syntax is shown below:

docker port [CONTAINER]

For example, either we can provide the container ID, and the 'docker port' command will output the information about the container's port. In addition to this, we can also specify the specific mapping for the container and verify if the port is associated with the container. Moreover, this is shown in the screenshot below. 18-docker port.png

Here we have specified port info for the container. Since the specified container does not have this port, the command outputs the appropriate message, as shown in the above screenshot.

docker top command

To display all the running processes of a container, we use the command 'docker top'. Additionally, its syntax is shown below:

docker top [CONTAINER]

The execution of the 'docker top' command is given in the below screenshot. 19- docker top.png

In the above screenshot, we can see the processes associated with the running container whose ID is specified as an argument for the 'docker top' command.

docker diff command

We can inspect changes to files or directories on a container's filesystem. Moreover, we can get the list of changed files and directories in a filesystem since creating the container using the command 'docker diff'. Additionally, the command has the following general syntax.

docker diff [CONTAINER]

docker image ls command

We can use the command 'docker image ls' to list all the images that are stored locally on a Docker system. Moreover, its syntax is:

docker image ls

In addition to the above, the output of the command execution is shown below. 20-list all images.png

We can see the command lists all the images present on the host. Additionally, the equivalent command 'docker images' also given the same output. 21-Get list of images.png

Comparing both outputs, we see that both commands behave similarly. So we can use either of the commands to get the list of images.

docker history command

The 'docker history' command shows the history of a docker image specified as an argument to the command.

docker history [IMAGE]

For example, if we want to know the history of the docker image named 'ubuntu', we can give the 'docker history ubuntu' command and obtain the output. 22-history.png

Consequently, as shown in the output, this command gives all the accounts of history associated with the specified image.

Docker Network Commands

We have already covered the Docker network commands in our 'Docker Networking' article. Additionally, docker containers connect and communicate with each other and to other non-Docker environments using Docker networking commands. Subsequently, to facilitate this networking, Docker provides various network commands as listed in the table below.

Docker Network Commands Usage
List Networks docker network ls
Remove one or more networks docker network rm [NETWORK]
Show network information docker network inspect [NETWORK]
Connect a container to a network docker network connect [NETWORK] [CONTAINER]
Disconnect container from a network docker network disconnect [NETWORK] [CONTAINER]

Key Takeaways:

  • Docker provides various commands to handle and manage the Docker ecosystem.
  • Additionally, major commands are categorized as per their usage for Containers, Images, Information, and Network.
Multi-stage builds in Docker
Multi-stage builds in Docker
Previous Article
Docker Swarm - Working and Setup
Docker Swarm - Working and Setup
Next Article
Shilpa Nadkarni
I am Shilpa Nadkarni, a freelance software professional, and content writer. I have around 10 years of experience in software development mainly in Microsoft Technologies. I am always learning new technologies and find myself up to date with the latest software technologies.
Ravinder Singh's Photo
Ravinder Singh

Similar Articles