“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Hello, I’m looking at the mountains.

This is the third part of Jakob Jenkov’s Docker Tutorial.

We can build Docker images, run Docker containers and push Docker images to remote locations through Docker commands and operations on Docker services. This article focuses on some common Docker commands.

Docker has many commands. This article will not cover all of them. If you need a complete command tutorial, you can refer to the Docker Command line Reference.

Please note that depending on how you install Docker on your Linux system, you may need to prefix all commands with sudo and run them as root. Such as:

sudo docker build .
Copy the code

. Not just:

docker build .
Copy the code

Docker command line tool

When Docker is installed in Linux, a command line tool named Docker is installed and can be executed on the Linux command line.

Docker has a lot of parameters. Different parameters have different functions and can instruct docker to make different behaviors. It can be considered as a command for docker. Here is a sample docker command:

docker build .
Copy the code

This example contains three parts: the docker command, the parameter build, and the parameter.

The build argument is a Docker command, in other words, a command to the Docker executable command line. Usually, the first argument to a Docker command line is a Docker command.

Argument. Is the argument to the build command.

docker build

Docker build command is to call docker from Dockerfile file to build docker image, to use docker build command, must tell it from which Dockerfile file to generate image. For more information about Dockerfile, see here. Here is an example of the docker build command:

docker build .
Copy the code

Parameter. To find the Dockerfile file from the current directory.

docker images

The docker images command lists native Docker images. Here is an example of the Docker images command:

docker images
Copy the code

Running the above command produces something similar to the following:

REPOSITORY       TAG        IMAGE ID        CREATED          SIZE
hello-world      latest     fce289e99eb9    9 months ago     1.84kB
Copy the code

docker run

The docker run command is used to run a docker container based on a given docker image. The parameter of docker run can be the name or ID of the Docker image. The following is an example of running a Docker container:

docker run hello-world
Copy the code

This example runs a Docker container based on a Hello-world image.

We can also use Docker image ID to run Docker container, the command is as follows:

docker run fce289e99eb9
Copy the code

docker ps

The docker ps command is used to display the docker containers that are running in the system, as shown in the following example:

docker ps
Copy the code

Note that some Docker containers close immediately after completing a task, and it is likely that such Docker containers will not be visible for a long time in the Docker ps result list.

【 translator’s Note 】 In fact, for the docker command line, we can first browse, know the general, and when there is a need, focus on the look. If not, forget, can be analogous to Linux command.

Recommended reading

  • Docker tutorial (1) : What is Docker
  • Docker tutorial (2) : Dockerfile
  • Docker tutorial (3) : Docker command

Hello, I’m looking at the mountains. Swim in the code, play to enjoy life. If this article is helpful to you, please like, bookmark, follow. Welcome to follow the public account “Mountain Hut”, discover a different world.