preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now

Tips

The interview guide series, which in many cases does not delve into the details, is a way for students to review their knowledge in the role of the interviewee, so I assume that most of the things you, as the interviewer, know.

www.processon.com/view/link/6…

This is the brain map address

Where t

Although the company uses containers, the container team is in charge of this one. As a business developer, you basically cannot understand so comprehensively. So today, Xiao Liu and everyone are going to learn about Docker.

Then below is a summary of previous articles

  • 2021-Java Backend Engineer Interview Guide (Introduction)
  • 2021-Java Backend Engineer Interview Guide
  • 2021-Java Backend Engineer Interview Guide -(Concurrency – Multithreading)
  • 2021-Java Backend Engineer Interview Guide -(JVM)
  • 2021-Java Backend Engineer Interview Guide -(MySQL)
  • 2021-Java Backend Engineer Interview Guide -(Redis)
  • Java Backend Engineer Interview Guide -(Elasticsearch)
  • 2021-Java Backend Engineer Interview Guide -(Message Queue)
  • 2021-Java Backend Engineer Interview Guide -(SSM)
  • 2021-Java Backend Engineer Interview Guide (SpringBoot+SpringCloud)
  • 2021-Java Backend Engineer Interview Guide -(Distributed Theory +Zookeeper)
  • 2021-Java Backend Engineer Interview Guide -(Computer Networking)
  • 2021-Java Backend Engineers must know -(Operating system)
  • (Distributed RPC Framework Dubbo)
  • 2021-Java Backend Engineers must Know -(Lunix)

Hello, everyone, I am Xiao Liu liu, currently in charge of the payment project of The First-tier Internet company with an annual revenue of over ten billion yuan. Thank you for your support. Today we will take a look at Docker

Docker profile

Why is a Docker

A product from development to launch, from the operating system, to the operating environment, and then to the application configuration. As collaboration between development + operations we need to care about many things, it’s also one of the many Internet companies have to face the problem, especially the various versions of iteration, different versions of environmental compatibility, is the test of all operations staff, don’t know what is you company, here is using the Docker container.

Docker has grown so fast because it provides a standardized solution to this problem. The environment configuration is so troublesome that it takes time and effort to start all over again with a different machine. Many people think, can fundamentally solve the problem, software can be installed with the environment? That is, when you install it, you copy exactly the original environment. Docker allows developers to eliminate the “works on my machine” problem with collaborative coding.

The concept of Docker

Docker is a cloud open source project based on Go language. The main goal of Docker is “Build, Ship and Run Any App,Anywhere”, that is, through the encapsulation, distribution, deployment, operation and other life cycle management of application components, The user’s APP (which can be a WEB application or database application, etc.) and its running environment can be “packaged once, run everywhere”.

The advent of Linux container technology solves such a problem, and Docker is developed on its basis. Running applications on Docker containers, which are consistent on any operating system, makes it cross-platform and cross-server. Only need to configure the environment once, switch to another machine can be deployed with one click, greatly simplify the operation

Why Docker

As an emerging virtualization method, Docker has many advantages over traditional virtualization methods

  • More efficient use of system resources
  • Faster startup time
  • Consistent operating environment
  • Continuous delivery and deployment
  • Easier migration
  • Compare with traditional virtual machines

Docker download

  • Docker website: www.docker.com
  • Docker Chinese website: www.docker-cn.com/
  • Docker Hub website: hub.docker.com/

Docker Installation (centos7)

Docker requires a CentOS kernel version later than 3.10. Check the prerequisites on this page to verify whether your CentOS version supports Docker.

Check your current kernel version with the uname -r command

 $ uname -r
Copy the code

2. Log in to Centos as user root. Make sure the YUM package is up to date.

$ sudo yum update
Copy the code

3. Uninstall the old version (if any)

$ sudo yum remove docker  docker-common docker-selinux docker-engine
Copy the code

Yum-util provides yum-config-manager functionality. The other two are dependent on devicemapper drivers

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Copy the code

5. Configure the yum source

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code

6. You can view all docker versions in all warehouses and select a specific version to install

$ yum list docker-ce --showduplicates | sort -r
Copy the code

7. Install Docker

# $sudo yum install docker-ce # $sudo yum install <FQPN> # Sudo yum install docker - ce - 17.12.0. CeCopy the code

8. Start and join boot

$ sudo systemctl start docker
$ sudo systemctl enable docker
Copy the code

9, verify whether the installation is successful (there are two parts of client and service, indicating that docker installation and startup are successful)

$ docker version
Copy the code

For a system using systemd, please write the following contents in /etc/dock/daemon. json (if the file does not exist, please create a new file).

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}
Copy the code

Then restart the service.

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
Copy the code

Docker mirror

As we all know, operating systems are divided into kernel and user space. For Linux, the root file system is mounted to provide user-space support after the kernel is started. A Docker Image, on the other hand, is a root file system. For example, the official ubuntu:16.04 image contains a complete set of root file systems for the ubuntu 16.04 minimum system.

Docker image is a special file system, in addition to providing programs, libraries, resources, configuration files required by the container runtime, but also contains some configuration parameters prepared for the runtime (such as anonymous volumes, environment variables, users, etc.). The image does not contain any dynamic data, and its contents are also after the build

A hierarchical

Because the image contains the complete root file system of the operating system, and its volume is often huge, Docker made full use of the technology of Union FS to design it as a layered storage architecture. So, strictly speaking, an image is not a packaged file like an ISO. An image is a virtual concept whose actual embodiment is not a file but a group of file systems, or a combination of multiple file systems.

When a mirror is built, one layer is built on top of the other. After each layer is built, there are no more changes, and any changes on the next layer only happen on your own layer. For example, deleting a file at the previous layer does not actually delete the file at the previous layer, but only marks the file as deleted at the current layer. This file will not be seen when the final container runs, but it will actually follow the image. Therefore, when building the image, extra care should be taken. Each layer should contain only what needs to be added to that layer, and any extras should be cleared away before the layer is built.

The feature of hierarchical storage also makes it easier to reuse and customize images. You can even use the previously built image as the base layer and then add new layers to customize what you need to build new images.

Docker command

The help command

  • docker version
  • docker info
  • docker –help

The mirror command

  • Docker images(lists images on localhost)
  • Docker search for a XXX image name
  • Docker pull name of XXX image
  • Docker rmI docker rmI docker RMI

Container order

  • Docker run [OPTIONS] IMAGE [COMMAND]
  • Docker ps [OPTIONS] (listing all containers currently running)
  • Docker start Container ID or container name
  • Docker restart Container ID or container name
  • Docker stop container ID or container name (stop container)
  • Docker kill container ID or container name (force stop container)
  • Docker rm container ID(delete stopped container)
  • Docker run -d
  • Docker logs -f -t –tail Docker logs -f -t –tail
  • Docker exec -it container ID bashShell (enter running container and interact with command line)
  • Docker cp Container ID: internal path Destination host path (copy files from the container to the host)

Docker pull pulls the image

  • The Docker Client receives the Docker pull command, parses the request and collects the request parameters, and sends an HTTP request to the Docker Server. The HTTP request method is POST, and the request URL is “/images/create? “+” XXX “;
  • Docker Server accepts the above HTTP request and delivers it to the Mux. Router, which determines the specific handler to execute the request through THE URL and request method.
  • Router dispatches the request route to the corresponding handler, PostImagesCreate;
  • In the PostImageCreate handler, a job named “pull” is created and executed.
  • During the execution of the job named “pull”, the pullRepository operation is executed, that is, one or more images are downloaded from the Docker Registry.
  • A job named “pull” gives the downloaded image to the Graphdriver;
  • Graphdriver is responsible for storing images. One side creates graph objects, and the other side records the relationship between images in GraphDB.

Docker run Hello-world

Docker data volume

What is the

If the data generated by the Docker container is not generated through the Docker commit, and the data is saved as part of the image, then when the container is deleted, the data will naturally be gone. In order to save the data, we use volumes in Docker.

In a word: similar to our Redis inside the RDB and AOF files, which is what we call persistent use

Data volume characteristics

Volumes are designed to persist data and are completely independent of the lifetime of the container, so Docker does not delete its mounted data volumes when the container is deleted

Features:

  • Data volumes can share or reuse data between containers
  • Changes in the volume take effect immediately
  • Changes in the data volume are not included in the update to the mirror
  • The life cycle of a data volume continues until no container can use it

In-container addition

  • Direct command Add

Docker run -v/host directory :/ The image name of the directory inside the container

  • DockerFile add

You can use the VOLUME directive in Dockerfile to add one or more data volumes to the image

Dockerfile

What is the

Dockerfile is a build file used to build a Docker image. It is a script composed of a series of commands and parameters.

Building three steps

  • Write a Dockerfile file
  • docker build
  • docker run

DockerFile build process parsing

Dockerfile content basics

  • Each reserved word instruction must be uppercase and followed by at least one argument
  • Instructions are executed from top to bottom
  • # comment
  • Each instruction creates a new image layer and commits the image

Docker executes the general process of Dockerfile

  • Docker runs a container from the base image
  • Executes an instruction and makes changes to the container
  • Commit a new mirroring layer by performing something similar to docker Commit
  • Docker then runs a new container based on the image it just submitted
  • Execute the next instruction in the dockerfile until all instructions have been executed

DockerFile architecture

  • FROM(base mirror, which mirror is the current new mirror based on)
  • MAINTAINER(Name and email address of the image MAINTAINER)
  • RUN(command to RUN when the container is built)
  • EXPOSE(the port that the current container exposes to the outside world)
  • WORKDIR(specifies the working directory that the terminal logs in to by default after creating the container, a landing point)
  • ENV(used to set environment variables during image building)
  • ADD(copy files from the host directory to the image and ADD command will automatically process the URL and decompress the tar package)
  • COPY(similar to ADD, copies files and directories to an image. Copy files/directories from the < source path > directory in the build context directory to the < destination path > location within the image in the new layer)
  • VOLUME(Container data VOLUME for data preservation and persistence)
  • CMD(specifies the command to run when a container is started)
  • ENTRYPOINT(the purpose of ENTRYPOINT, like CMD, is to specify the container launcher and parameters)
  • ONBUILD(run the command when building an inherited Dockerfile and the parent image’s ONBUILD is triggered when the quilt inherits the parent image)

Local image is published to Ali Cloud

Image generation method

  • The previous DockerFile
  • Docker commit [OPTIONS] Container ID [REPOSITORY[:TAG]]

Push the local image to Ali Cloud

Ali Cloud developer platform cr.console.aliyun.com/cn-shanghai…

Push the image to Registry

  • docker login –username=danielyoungchina registry.cn-shanghai.aliyuncs.com
  • Docker tag (ImageId) registry.cn-shanghai.aliyuncs.com/daniel-hub/nginx-docker: [the mirror version number]
  • The docker push registry.cn-shanghai.aliyuncs.com/daniel-hub/nginx-docker: [the mirror version number]

Pull an image from Registry

  • The docker pull registry.cn-shanghai.aliyuncs.com/daniel-hub/nginx-docker: [the mirror version number]

The end of the

Here is a summary of the basic knowledge of Docker, because why do you prepare this for an interview? As we know, the service governance of K8S is basically now, and the release of the service is basically based on containers. It may be very simple when the company uses it, just click the upgrade button, for example, below

But? We still need to understand this thing by ourselves, so that problems can be solved quickly.

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Wechat search “six pulse Excalibur program life” reply 888 I find a lot of information to you