This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Get to know Docker

A simple example

At the end of the last article, you are advised to install Docker yourself according to the official installation documentation. At the end of the steps, you can verify the installation by running the following command:

➜  ~ docker run hello-world
Copy the code

If there are no problems, the console prints something like the following:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:37a0b92b08d4919615c3ee023f7ddb068d12b8387475d64c622ac30f45c29c51
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
Copy the code

Docker does not find hello-world:latest image locally, so remote pull this image, where latest can be read as the version number. If the version number is not specified when executing the command, the latest version will be used, which is latest.

Next, Hello from Docker! To generate this message, Docker took the following steps: This section introduces what Docker does to make this information visible:

  1. Docker client is connected to the Docker daemon.
  2. Docker guardianThe process fromDocker HubPull thehello-world The mirror.
  3. The Docker daemon creates a container from this image that runs the executable and produces what you are reading now.
  4. The Docker daemon transmits the output stream to the Docker client, which sends it to your terminal.

This is how Docker creates the container with the Hello-world image and runs it. Here are some of the key concepts.

Docker client and daemon

Docker is a software with C/S architecture. When we use Docker commands on the command line, the command line tool of Docker will send requests to the Docker daemon and then display the received results on the terminal.

When we install Docker according to the previous steps, its server (Docker daemon) and client (Docker command line tool) are installed on the same machine. You can also remotely connect the Docker client of one machine to the Docker daemon of another machine.

The mirror Image

A container is essentially a binary file that contains a program and the environment and configuration it needs to run. An image is built using a set of instructions, such as adding a file, executing a command, opening a port, etc., and the contents of the image are immutable after the build is complete.

In real development, one image often inherits from the other, and then adds more instructions to build a new image. For example, you can build your own new image based on an image that contains the Java runtime environment, plus your own Java programs.

Registry and Docker Hub

Docker uses Registry to store images built by users. Docker operates a public Registry called Docker Hub, which can be understood as a GitHub for storing and sharing images.

Many enterprises also deploy private Docker Registry for internal use.

Container, the Container

A container is an instance of a mirror running. A container can run one or more processes. We can instantiate multiple containers based on a single image, similar to the relationship between classes and objects in object-oriented programming.

A container can be created, started, terminated, deleted, and when a container is terminated, it is not deleted and can be started again.

Some containers terminate on their own after they run, such as the container generated by the Hello-world image at the beginning of this article, and some containers continue to run unless they are manually planted and must provide network services.