· Tutorials · 4 min read
Docker for beginners - Part 3 - Docker CLI
Docker CLI is the command-line interface for Docker. It allows you to interact with Docker daemon using commands. You can use Docker CLI to build, run, and manage Docker containers. In this tutorial, we will learn about some of the most commonly used Docker CLI commands.
Series Overview
- Part 1 - Docker for beginners - Introduction and Installation
- Part 2 - Docker for beginners - Docker Images and Containers
- Part 3 - Docker for beginners - Docker CLI (You are here)
- Part 4 - Docker for beginners - Dockerfile
- Part 5 - Docker for beginners - Docker Compose
Docker CLI
Docker CLI is the command-line interface for Docker. It allows you to interact with Docker daemon using commands. You can use Docker CLI to build, run, and manage Docker containers. While most of the common operations can be done from Docker Desktop, you can also use Docker CLI to perform the same operations, which is useful when you are working on a remote machine. In this tutorial, we will learn about some of the most commonly used Docker CLI commands and Dockerfile.
docker pull
docker pull
command is used to pull a Docker image from a Docker registry, to the local machine. For example, to pull the nginx
Docker image, run the following command.
docker pull nginx
If you don’t specify the version of the Docker image, it will pull the latest version of the Docker image. You can also specify the version of the Docker image to pull. For example, to pull the nginx
Docker image with version 1.19.10
, run the following command.
docker pull nginx:1.19.10
You can also pull the nginx
Docker image with version 1.19
by running the following command.
docker pull nginx:1.19
docker run
docker run
command is used to run a Docker container from a specified Docker image. For example, to run the nginx
Docker container, run the following command.
docker run nginx
You can use the --name
option to specify the name of the Docker container. For example, to run the nginx
Docker container with the name nginx-container
, run the following command.
docker run --name nginx-container nginx
You can also use the -d
option to run the Docker container in the background. For example, to run the nginx
Docker container in the background with the name nginx-container
, run the following command.
docker run -d --name nginx-container nginx
Note: If you don’t have the
nginx
Docker image, it will automatically pull thenginx
Docker image from the Docker registry.
docker ps
docker ps
command is used to list the running Docker containers. You can use the -a
option to list all the Docker containers. For example, to list all the Docker containers, run the following command.
docker ps -a
docker stop
docker stop
command is used to stop a running Docker container. You can use the -t
option to specify the number of seconds to wait before stopping the Docker container. For example, to stop the nginx-container
Docker container, run the following command.
docker stop nginx-container
docker rm
docker rm
command is used to remove a Docker container. You can use the -f
option to force remove the Docker container. For example, to remove the nginx-container
Docker container, run the following command.
docker rm nginx-container
docker rmi
docker rmi
command is used to remove a Docker image. You can use the -f
option to force remove the Docker image. For example, to remove the nginx
Docker image, run the following command.
docker rmi nginx
docker exec
docker exec
command is used to execute a command inside a running Docker container. For example, to execute the ls
command inside the nginx-container
Docker container, run the following command.
docker exec nginx-container ls
docker logs
docker logs
command is used to view the logs of a Docker container. For example, to view the logs of the nginx-container
Docker container, run the following command.
docker logs nginx-container
docker inspect
docker inspect
command is used to view the details of a Docker container. For example, to view the details of the nginx-container
Docker container, run the following command.
docker inspect nginx-container
docker build
docker build
command is used to build a Docker image from a Dockerfile. For example, to build a Docker image from the Dockerfile
in the current directory, run the following command.
docker build -t my-image .
We will soon learn about Dockerfile in the next section.
docker push
docker push
command is used to push a Docker image to a Docker registry. For example, to push the my-image
Docker image to the Docker registry, run the following command.
docker push my-image
Wrapping up
In this tutorial, we learned about Docker CLI in detail. We also learned about some of the most commonly used Docker CLI commands. In the next tutorial, we will learn about Dockerfile.