Docker Command-Line Interface

Docker Command-Line Interface

In this tutorial, we have learned how to use the Docker Command Line Interface to download and use various images. The upcoming tutorials will be to learn how to use Docker Compose to run multiple containers. To learn Docker more deeply, check out the official Docker documentation.

The syntax of the Docker CLI command is as follows:

docker [Option] [Sub-Command] [Arguments]

To see the list of available commands of Docker, simply type the docker on the terminal and then press Enter key.

docker

If you need a brief explanation of the Docker Sub-Commands. Use --help with docker.

docker [subcommand] --help

Docker Images

A Docker image is a file that contains a set of instructions to build a Docker container. A Docker image is a file used to execute code inside the Docker container. Docker image contains the application with all its dependencies such as necessary instructions, libraries, and binaries to run the application. Docker image is like a snapshot of a virtual machine (VM) environment.

Various types of Docker images are already available on the Docker Hub. Docker Hub is a cloud-based service that is used to save your Docker image on the Docker hub repositories. You can share your docker image with everyone.

Search Docker Image

If you want to search the required image on the Docker Hub registry. Use the search sub-command

For example, if you want to search for a CentOS image. You the below command

docker search centos

The output will look like this:

Docker Command-Line Interface

Most of the Docker images are tagged with a version. If an image does not have any tag specified, then Docker will consider it as the latest one.

Download Docker Image

To download the latest build of CentOS image, then use the image pull command:

docker image pull centos
docker image pull centos

If you have not mentioned the image version then docker will download the latest version of the image with all its dependencies.

To list all downloaded images, use the following command

docker image ls

The output will look like this:

Docker Command-Line Interface

Remove Docker Image

If you want to remove an image, then use the image rm (image_name)

docker image rm centos
Docker Command Line Interface

Docker Containers

A Docker container is a standard unit of software that binds up the code with all its dependencies so that the application runs very quickly. With a docker container, the application can run in any environment. Docker image is a standalone, lightweight executable package of the software which includes application contents (runtime, code, system libraries, setting, and code).

Manage the Docker services

We can easily start and stop the Docker services with the following commands.

Start Docker Container

To start the Docker container-based CentOS image, run the below command. First, The command will check the CentOS image locally. If the command is not able to find the CentOS image in local, then it will download it.

docker container run centos

After running the above command, it will look like nothing happens, but that is not true. The CentOS container starts and stops immediately because the empty container can not run for a long time. To run the container for a long time, we need to provide the command.

To interact with the container via command line, we need to use -it argument with Docker command.

docker container run -it centos /bin/bash

The output will look like this.

[root@599ecafa6e39 /]

Now you are working inside the docker container.

List Docker Containers

To check the active docker containers, enter the following command.

docker container ls
Docker Command Line Interface

If you don’t have any active containers, then the output will look like this.

To check all the containers active as well as inactive, run the same command with -a argument.

docker container ls -a
Docker Command Line Interface

Remove Docker Containers

To remove one or more containers, then use the following command with the container ID(or IDs) which you want to delete.

docker container rm 6337713c037e

Conclusion

In this tutorial, we have learned how to use the Docker Command Line Interface to download and use various images. The upcoming tutorials will be to learn how to use Docker Compose to run multiple containers. To learn Docker more deeply, check out the official Docker documentation.

If you guys have any queries related to Docker, then let me know in the comments.