Docker (I): introduction to docker

Time:2021-9-25

Life is boundless, learning is boundless

preface

Nowadays, docker is widely used, especially in front-line Internet companies. Using docker technology can help enterprises expand services quickly and horizontally, so as to achieve the ability of elastic deployment of business. After the rise of the cloud service concept, the usage scenario and scope of docker have further developed. Now, with the increasing popularity of micro service architecture, the perfect combination of micro service and docker makes it easier for the operation and maintenance deployment of micro service architecture.

Before understanding docker, let’s consider a few questions: 1. What is docker? 2. Why use docker? What are its advantages?

What is docker?

Docker is a kind of encapsulation of Linux container and provides a simple and easy-to-use container interface. It is currently the most popular Linux container solution.

Docker packages the application and its dependencies in a file. Running this file will generate a virtual container. The program runs in this virtual container as if it were running on a real physical machine. With docker, you don’t have to worry about environmental problems.

Overall, docker’s interface is quite simple. Users can easily create and use containers and put their applications into containers. The container can also perform version management, copying, sharing and modification, just like managing ordinary code.

Why use docker

1. Faster delivery and deployment

For development and operation and maintenance (devop) personnel, what they want most is to create or configure it at one time, and it can run normally anywhere.

Developers can use a standard image to build a set of development containers. After development, operation and maintenance personnel can directly use this container to deploy code. Docker can quickly create containers, quickly iterate applications, and make the whole process visible, making it easier for other members of the team to understand how applications are created and work. Docker container is light and fast! The startup time of the container is seconds, which greatly saves the time of development, testing and deployment.

2. More efficient virtualization

The operation of docker container does not require additional hypervisor support. It is kernel level virtualization, so it can achieve higher performance and efficiency.

3. Easier migration and expansion

Docker container can run on almost any platform, including physical machine, virtual machine, public cloud, private cloud, personal computer, server, etc. This compatibility allows users to migrate an application directly from one platform to another.

4. Simpler management

Using docker, you can replace a large number of previous updates with only a small modification. All modifications are distributed and updated incrementally, so as to realize automatic and efficient management.

Docker vs VM

As can be seen from the figure below, VM is a complete operating system running on the host. Running its own operating system will occupy more CPU, memory and hard disk resources. Unlike VM, docker only contains applications and dependent libraries. Based on libcontainer, it runs on the host and is in an isolated environment, which makes docker lighter and more efficient. It only takes a few seconds to start the container. Because docker is lightweight and occupies less resources, docker can be easily applied to build standardized applications. However, docker is not perfect at present. For example, the isolation effect is not as good as VM, and some basic libraries of the host operating system are shared; The network configuration function is relatively simple, mainly in bridge mode; It is not convenient and flexible to view logs.

Docker (I): introduction to docker

Docker further encapsulates the container, from file system, network interconnection to process isolation, which greatly simplifies the creation and maintenance of the container. This makes docker technology lighter and faster than virtual machine technology.

As a new virtualization method, docker has many advantages over traditional virtualization methods. The start of docker container can be realized in seconds, which is much faster than the traditional virtual machine method; Docker has a high utilization of system resources. Thousands of docker containers can be run on a host at the same time.

Docker (I): introduction to docker

Related concepts

Docker is a CS architecture with two main concepts:

  • Docker daemon: running on the host machine, the docker daemon, and the user interacts with the docker daemon through the docker client (docker command)
  • Docker client: docker command line tool is the main way for users to use docker. Docker client communicates with docker daemon and returns the results to users. Docker client can also access remote docker daemon through socket or restful API

Docker (I): introduction to docker

After understanding the composition of docker, let’s understand the three main concepts of docker:

  • Docker image: the image is read-only and contains files that need to be run. Images are used to create containers. One image can run multiple containers; Images can be created through dockerfile or downloaded from docker hub / registry.
  • Docker container: container is the running component of docker. Starting an image is a container. Container is an isolated environment. Multiple containers will not affect each other to ensure that the programs in the container run in a relatively safe environment.
  • Docker hub/registry: share and manage docker images. Users can upload or download the images above. The official address ishttps://registry.hub.docker.com/You can also set up your own private docker registry.

The image is equivalent to the packaged version. After the image is started, it runs in the container, and the warehouse is the place where the image is stored.

Docker installation

It is recommended to install docker in Linux environment. The window environment is complex and error prone. It is convenient to install docker environment using centos7 + yum.

Here I buy Alibaba cloud servers, which can be done for a few hundred yuan. There is a big discount for new users.

First, check the server version and configuration:

[[email protected] /]# uname -r
3.10.0-1062.18.1.el7.x86_64
[[email protected] /]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1. Uninstall old version

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Required installation package

yum install -y yum-utils

3. Set up a mirrored warehouse

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo   #The default is foreign
    
yum-config-manager \
    --add-repo \
 http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo   #Alibaba cloud image warehouse (recommended)
 
 [[email protected] /]# yum-config-manager \
>     --add-repo \
>  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

4. Update Yum package index

yum makecache fast

5. Install docker CE community EE enterprise

yum install docker-ce docker-ce-cli containerd.io

6. Start docker

systemctl start docker

7. Using docker version

[[email protected] /]# docker version
Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:33:55 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:32:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

8、hello-world

[[email protected] /]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:89b647c604b2a436fc3aa56ab1ec515c26b085ac0c15b0d105bc475be15738fb
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/

9. View downloaded images

[[email protected] /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    d1165f221234   3 days ago   13.3kB

10. Uninstall docker

yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

Alibaba cloud image accelerator

1. Log in to alicloud and find the container image service

Docker (I): introduction to docker

2. Mirror accelerator found

Docker (I): introduction to docker

3. Configuration use

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://0uf1zwdk.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

reference resources

Docker – from entry to practice

Docker Series 1: Introduction

Docker tutorial