Docker CLI
In this article, we will explore the basics of Docker CLI, its commands, and best practices for using it effectively. We will also discuss the importance of understanding Docker CLI and how it fits into the larger world of containerization. By the end of this article, you will have a solid understanding of Docker CLI and be able to use it to manage your Docker containers and images more efficiently. So, let’s dive in!
1. Introduction
Docker has revolutionized the world of software development by enabling developers to containerize their applications and easily deploy them in different environments. Docker is an open-source platform that allows developers to package their applications and dependencies into a container, which can be run consistently across different machines. Docker CLI (Command-Line Interface) is a powerful tool that helps developers to manage Docker containers and images through the terminal.
2. Understanding Docker CLI
2.1 What is Docker CLI?
Docker CLI is a command-line tool that allows developers to interact with Docker through the terminal. It is the primary interface that developers use to manage their Docker containers, images, networks, and volumes. It is built on top of Docker’s REST API and communicates with the Docker daemon to execute various commands. Developers can use it to perform a wide range of tasks, such as running containers, building images, managing networks, and more.
2.2 Difference between Docker CLI and Docker API
Docker API is a RESTful web service that allows developers to interact with Docker programmatically. Developers can use Docker API to create, manage, and monitor Docker containers, images, networks, and volumes through HTTP requests. Docker CLI, on the other hand, is a command-line tool that allows developers to interact with Docker through the terminal. It provides a convenient way for developers to use Docker API without having to write complex HTTP requests.
2.3 Importance of Docker CLI in containerization
Docker CLI is an essential tool for containerization because it allows developers to manage Docker containers and images efficiently. It provides developers with a straightforward way to create, start, stop, and delete containers. It also enables developers to build, tag, and push Docker images to registries, such as Docker Hub or Amazon ECR. By using Docker CLI, developers can streamline their workflow and reduce the time it takes to build, test, and deploy their applications.
3. Docker CLI Commands
3.1 Docker run
The docker run command is used to create and start a new container based on a specified image. This command is one of the most commonly used Docker CLI commands and is essential for running applications in Docker containers. The docker run command allows developers to specify various options, such as environment variables, volumes, and ports.
The basic docker run command takes this form:
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
The docker run command must specify an IMAGE to derive the container from. With the docker run [OPTIONS]
an operator can add to or override the image defaults set by a developer. And, additionally, operators can override nearly all the defaults set by the Docker runtime itself. The operator’s ability to override image and Docker runtime defaults is why run has more options than any other docker
command. To learn how to interpret the types of [OPTIONS]
, see Option types.
Depending on your Docker system configuration, you may be required to preface the docker run
command with sudo
. To avoid having to use sudo
with the docker
command, your system administrator can create a Unix group called docker
and add users to it.
3.2 Docker build
The docker build command is used to create a new Docker image based on a Dockerfile. A Dockerfile is a text file that contains instructions for building a Docker image. The docker build command allows developers to specify various options, such as tags, build contexts, and cache settings.
Docker usage command
docker build --help
It prints the following output:
The docker build
command builds Docker images from a Dockerfile and a context. In order to specify a context, you need to reference a local file directory or a URL. The contents of this context can be referenced from ADD
instructions of the Dockerfile to reference a file in the context.
3.3 Docker images
To list Docker images on your machine, you can use the Docker command-line interface (CLI) tool. The Docker CLI tool provides a variety of commands that you can use to manage Docker images, containers, and networks. The most commonly used command to list Docker images is docker images
.
docker images [OPTIONS] [REPOSITORY[:TAG]]
Here are the options that you can use with the docker images
command:
-a
or--all
: Shows all images, including the intermediate images that are used to build the final image.--digests
: Shows the image digests instead of the image tags.--format
: Shows the images in a custom format using Go template syntax.-q
or--quiet
: Shows only the image IDs.
To list all Docker images on your machine, run the following command:
docker images
This command will list all the images on your machine, along with their repository, tag, and size.
3.4 Docker ps
The docker ps command is used to list all the running Docker containers on a machine. This command provides developers with information about the containers, such as the container ID, status, and name.
docker ps
The default invocation of the docker ps
command without any options lists out all containers in running state
3.5 Docker stop
The docker stop command is used to stop a running Docker container. This command sends a SIGTERM signal to the container, giving it time to shut down gracefully before being forcibly terminated.
The command to stop a single container in Docker is:
Command syntax
docker stop your-container-id
Replace your-container-id with the actual ID or name of the container you want to stop. For example, if you want to stop a container with an ID of my-nginx
, you can run the following command:
Example
docker stop my-nginx
4. Conclusion
In conclusion, Docker CLI is an essential tool for managing Docker containers and images. Developers can interact with Docker through the terminal, perform various tasks, and streamline their workflow. By understanding Docker CLI and its commands, developers can create, start, stop, and delete containers, build and tag images, and manage networks and volumes. It provides a convenient way for developers to use Docker API without having to write complex HTTP requests.