Docker Commands

Docker is an application that offers a set of platform-as-a-service products for developing and deploying applications by packaging software in containers. Docker is written in the Go language which is similar to C used by the developers in writing infrastructure software. In order to run the Docker Commands make sure that you have successfully installed Docker on your system as a prerequisite. Before proceeding with the basic commands, go through the Docker toolset that consists of Docker Daemon, Docker Client, and Docker Hub. When you start working with the docker client, the commands are sent to the Docker Daemon, which then interprets those commands and executes them for you.

This blog will summarize the basic commands and comprises the topics which include docker image, containers, and list of docker commands. Containers are lightweight, portable, virtual environments that developers will share without risking inconsistencies in development. Because of these magnificent features, many organizations have switched from using virtual machines to Docker containers. Docker presents users with new service-related terminologies to get familiarized with the concepts such as Dockerfiles, images, containers, and other Docker-specific words. Once the vocabulary is mastered then you can start using Docker commands. A list of all the commands and options is quite extensive.

Docker Client Help

The docker client comprehends several commands. Let us have a look at various commands that you will primarily use while running docker.

Apply the following command to get help:

$ docker help

Executing the “help” command provides you a full list of commands that the docker client can understand. Most of the commands are self-explanatory and are used while dealing with containers.

You can get more help with the use of any COMMAND, apply the following command as shown below:

$ docker COMMAND --help

The initial list of commands

In this section, you will try executing various commands. In the terminal type the following command.

$ docker version

Executing the above command will display the current version of the Docker. 

$ docker info

Executing the “info” command displays several pieces of information about the OS.

How to Run Unix Commands/Utilities?

Let us run a few Unix commands/utilities to get a bit familiar with them. Begin with the following steps as follows:

  1. There is a useful Docker Image called busybox (similar to hello-world) that someone has already created for Docker.
  2. The docker run command is used to run a container to create an instance of that image.
  3. By running the container, you will run some commands inside it.

Let us now go through these steps to execute the docker commands, we are not going to run only the necessary commands to get you a bit familiar with the commands. We will be looking at some of these commands in more detail in subsequent sessions.

$ docker search busybox

This command will search the online Docker registry for an Image named busybox. A similar output will be displayed on the screen with the following top few rows.

IMAGE

Let us comprehend the output by analyzing the columns:

  1. The first column is NAME which gives you the name of the Docker image.
  2. The second column is DESCRIPTION which is used to describe as usual.
  3. The next column is STARS and you will observe the list of images that matched the search term Docker has been listed in the descending order of the number of people who have starred the project. This is a very useful indicator of the popularity/correctness of the Image. 

The first column is the NAME of the Docker image which is set to reiterate. This is a unique name and that is used for some of the commands given below.

Now think the busybox image name is fine and you would like to create an instance (Container) of this image. To do so, all you need to do is use the docker run command as shown below:

$ docker run -t -i busybox

The run command would assist you to understand the architecture of Docker. The run command does the following:

  1. The command checks if you already have a busybox image in your local repository.
  2. If it is not found, then it will pull the image from the Docker hub. Pulling the image is relevant to downloading it which could take a while.
  3. After the pull becomes successful, it is now present in your local repository, and therefore it is then able to create a container based on this image.
  4. We have provided -i -t as the parameters to the run command which means to say it is interactive and attaches the tty input.

Note that if the image was present locally, it would directly run the container for you. On successful launch of the container, you will be directed into the bash shell for the busybox. To convey this simply, we are now logged into the busybox container and are at the command prompt, as shown below:

Docker Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

IMAGE

You can then run some of the following commands as shown below:

IMAGE

When you are inside the container from the prompt. You can exit the container by simply passing the exit command.

IMAGE

After providing the exit command, the container will stop running. You can verify this by passing another command as shown below.

$ docker ps

IMAGE

This provides you a list of all the running containers. You will observe that there is no container running in the output.

You can try this, If you are willing to discover the containers that were running earlier but are not in the terminated state, you can use the -all flag for the docker ps command. 

How to get the list of Docker images?

If you would like to know which images are already present on your docker setup locally, type the following command:

$ docker images

You will then find it has the busybox listed. Notice the columns which the output presents, two of the important columns are given below. 

  1. REPOSITORY
  2. TAG

The REPOSITORY column is clearly evident which describes the name of the Image. The TAG is also important, you will notice that the TAG value is mentioned as the latest. 

When you had provided the following command earlier ”$ docker run -t -i busybox”, it means that we have only specified the name and by default, if just the IMAGE name is specified, then it gets the latest image by default. The tag value “latest” is sort of implicitly used by the Docker client in the absence of an explicit tag value provided by you.

To put it in another way, you could have provided it as shown below:

$ docker run -t -i busybox:latest

Likewise, there is a clear likelihood that there will be multiple versions of any image present in the Docker Hub. We will see all that in some time, but as of now think that there were only the following versions available of busybox:

  1. Image Name : busybox, Version TAG : 1.0
  2. Image Name : busybox, Version TAG : 2.0
  3. Image Name : busybox, Version TAG : 3.0

We could mention the version TAG as shown below and so on as required:

$ docker run -t -i busybox:1.0

$ docker run -t -i busybox:2.0

Docker Containers

While executing the docker ps command, you will observe that no container was running. The reason is that you have applied exit out of the container. This means to say that the container would exist as long as its parent process is running.

Now, let us type the “docker ps -all” command. This would display the container that was launched a few minutes ago by you. For instance, you will see a similar screen on your system.

IMAGE

You will now see the following columns after executing the above command:

  • CONTAINER_ID: It is a unique ID for the container that was launched.
  • IMAGE: This was the IMAGE which you have launched i.e. busybox.
  • COMMAND: This is a default command which is executed when the container is launched. When the container based on a busybox image was launched, it directed you to the Unix Prompt that launched a Shell. This is what exactly the program in /bin/sh does. This would provide a clue to you that in any circumstance if you need to package your own Server in a Docker image, your default command here would typically be the command to launch the Server and put it in a listening mode.

Relaunch a Container

You can start a stopped container by using the docker start command. All you must do is to provide the Container ID to the docker start command.

So, view the docker ps -all output and note down the CONTAINER_ID. The docker ps -all command will give you a similar output as shown below.

IMAGE

You can then note down the CONTAINER_ID and provide the following command:

$ docker start ab0e37214358

The parameter “-i” is used to get into interactive mode. If everything goes fine then you can see that the Container gets restarted and it will get back to you again at the Prompt, as shown below:

IMAGE

Type exit to come out of the container, this would take you back to where we have started with no containers running.

Ops Trainerz

Subscribe to our youtube channel to get new updates..!

Attach a running container

Let us do some experiments to understand a few more commands. We will continue with the same example around busybox Image. 

First, relaunch the container without the -i (interactive) mode and provide the following command:

$ docker start ab0e37214358

ab0e37214358

You will see that you will get only the CONTAINER ID back as output. What happened is that the Container was launched and what the docker client has done is it has returned only the Container ID back. Provide the following command to check out the currently running containers.

$ docker ps

IMAGE

This provides the above similar output running the CONTAINER, check the STATUS column that displays as turns Up! We can attach to a running Container through the docker attach command. Let us attach to it:

$ docker attach ab0e37214358

This will get back to the Prompt where you will be inside the busybox container. Type exit command to exit the container and then try the docker ps command. There will be no containers that will be running.

If you would like to stop a running container, you can pass the following command as “docker stop ”. 

List of Docker Commands

The following list briefly describes the docker commands for Image, Container, Volume, Network, docker-compose, docker-machine, Swarm.

Docker Container Commands

In this section, you will find the most dominant commands related to the lifecycle of Docker containers.

Creating a container without starting it:

docker create [IMAGE]

Rename an existing container:

docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]

Run a command in a new container:

docker run [IMAGE] [COMMAND]

docker run --rm [IMAGE] – removes a container after it exits.

docker run -td [IMAGE] – starts a container and keeps it running.

docker run -it [IMAGE] – starts a container, allocates a pseudo-TTY connected to the container’s stdin, and creates an interactive bash shell in the container.

docker run -it-rm [IMAGE] – creates, starts, and runs a command inside the container. Once it executes the command, the container is removed.


Delete a container when it is not running:

docker rm [CONTAINER]


Update the configuration of one or more containers:

docker update [CONTAINER]


Starting and Stopping Containers Commands

You can view the following commands which explain how to start and stop the processes in a particular container.


Start a container:

docker start [CONTAINER]


Stop a running container:

docker stop [CONTAINER]


Stop a running container and start it up again:

docker restart [CONTAINER]


Pause processes in a running container:

docker pause [CONTAINER]


Unpause processes in a running container:

docker unpause [CONTAINER]


Block a container until others stop (after which it prints their exit codes):

docker wait [CONTAINER]


Kill a container by sending a SIGKILL to a running container:

docker kill [CONTAINER]


Attach local standard input, output, and error streams to a running container:

docker attach [CONTAINER]


Docker Image Commands

The following are the commands which are used while working with Docker images.


Creating an image from a Dockerfile:

docker build [URL]

docker build -t – builds an image from a Dockerfile in the current directory and tags the image


Pull an image from a registry:

docker pull [IMAGE]


Push an image to a registry:

docker push [IMAGE]


Create an image from a tarball:

docker import [URL/FILE]


Create an image from a container:

docker commit [CONTAINER] [NEW_IMAGE_NAME]


Remove an image:

docker rmi [IMAGE]


Load an image from a tar archive or stdin:

docker load [TAR_FILE/STDIN_FILE]


Save an image to a tar archive, streamed to STDOUT with all parent layers, tags, and versions:

docker save [IMAGE] > [TAR_FILE]

Docker Training

Weekday / Weekend Batches

Docker Container and Image Formation Commands

Once your container set up is done, you have to know how to get all the significant information to manage them. The following commands will provide details on images and containers on your system.

List running containers:

docker ps

docker ps -a – lists both running containers and ones that have stopped


List the logs from a running container:

docker logs [CONTAINER]


List low-level information on Docker objects:

docker inspect [OBJECT_NAME/ID]


List real-time events from a container:

docker events [CONTAINER]


Show port (or specific) mapping for a container:

docker port [CONTAINER]


Show running processes in a container:

docker top [CONTAINER]


Show live resource usage statistics of containers:

docker stats [CONTAINER]


Show changes to files (or directories) on a filesystem:

docker diff [CONTAINER]


List all images that are locally stored with the docker engine:

docker image ls


Show the history of an image:

docker history [IMAGE]


Network Commands

The significant feature of Docker is the ability to connect containers to each other and to other non-Docker workloads. This section covers the commands related to the network.


List networks:

docker network ls


Remove one or more networks:

docker network rm [NETWORK]


Show information on one or more networks:

docker network inspect [NETWORK]


Connects a container to a network:

docker network connect [NETWORK] [CONTAINER]


Disconnect a container from a network:

docker network disconnect [NETWORK] [CONTAINER]

Conclusion:
Thus we now got familiarized with the topics on how to use an image, create and launch containers by executing the basic docker commands.  This list of commonly used Docker commands will assist you to acquaint all those concepts of creating and managing containers, images, and networks.

Krishna
Krishna
AWS Lambda Developer
I am working as AWS Lambda Developer since 2014. and I have good knowledge skills on AWS & DevOps Platform. TO share my Knowledge through Blogs from OpsTrainerz is Good Opportunity to Me.

Request for more information