跳到主要内容
  1. 所有文章/
  2. Docker学习笔记/

Docker01安装配置和基本原理

·📄 1092 字·🍵 3 分钟

安装 #

环境准备: #

  1. Linux基础
  2. Linux操作系统
  3. 使用 Xshell 连接远程服务器

查看环境: #

#系统内核是3.10以上的
[root@izwz91mv6i3x6k12jjqh60z ~]# uname -r
3.10.0-514.26.2.el7.x86_64

#查看系统信息
[root@izwz91mv6i3x6k12jjqh60z ~]# 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"

安装教程: #

可以查看官网文档:https://docs.docker.com/engine/install/

image-20210423220917139.png

对于 CentOS 系统:

#先卸载旧版本Docker系统(如果有的话)
[root@izwz91mv6i3x6k12jjqh60z ~]# sudo yum remove docker \
                   docker-client \
                   docker-client-latest \
                   docker-common \
                   docker-latest \
                   docker-latest-logrotate \
                   docker-logrotate \
                   docker-engine
                   
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal


#安装需要的安装包
[root@izwz91mv6i3x6k12jjqh60z ~]# yum install -y yum-utils

Installed:
  yum-utils.noarch 0:1.1.31-54.el7_8                                                                                                           

Dependency Installed:
  libxml2-python.x86_64 0:2.9.1-6.el7.5           python-chardet.noarch 0:2.2.1-3.el7           python-kitchen.noarch 0:1.1.1-5.el7          

Dependency Updated:
  libxml2.x86_64 0:2.9.1-6.el7.5                                                                                                               

Complete!

#配置下载Docker镜像
sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

#安装docker,一路输入确定安装即可
sudo yum install docker-ce docker-ce-cli containerd.io


Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                                  checkpolicy.x86_64 0:2.5-8.el7                                       
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8                      docker-ce-rootless-extras.x86_64 0:20.10.6-3.el7                     
  docker-scan-plugin.x86_64 0:0.7.0-3.el7                                 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8                                
  fuse3-libs.x86_64 0:3.6.1-4.el7                                         libcgroup.x86_64 0:0.41-21.el7                                       
  libseccomp.x86_64 0:2.3.1-4.el7                                         libsemanage-python.x86_64 0:2.5-14.el7                               
  policycoreutils-python.x86_64 0:2.5-34.el7                              python-IPy.noarch 0:0.75-6.el7                                       
  setools-libs.x86_64 0:3.3.8-4.el7                                       slirp4netns.x86_64 0:0.4.3-4.el7_8                                   

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7                             audit-libs.x86_64 0:2.8.5-4.el7         libselinux.x86_64 0:2.5-15.el7               
  libselinux-python.x86_64 0:2.5-15.el7                  libselinux-utils.x86_64 0:2.5-15.el7    libsemanage.x86_64 0:2.5-14.el7              
  libsepol.x86_64 0:2.5-10.el7                           policycoreutils.x86_64 0:2.5-34.el7     selinux-policy.noarch 0:3.13.1-268.el7_9.2   
  selinux-policy-targeted.noarch 0:3.13.1-268.el7_9.2   

Complete!

注意:Centos8的服务器,可能会报错:

CentOS-8 - AppStream                                                                                      7.6 kB/s | 2.3 kB     00:00    
Errors during downloading metadata for repository 'AppStream':
  - Status code: 404 for http://mirrors.cloud.aliyuncs.com/centos/8/AppStream/x86_64/os/repodata/repomd.xml (IP: 100.100.2.148)
Error: Failed to download metadata for repo 'AppStream': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried

原因是:Centos8官方源已下线,需要切换成Centos-vault源:

# 进入yum目录
cd /etc/yum.repos.d

# 删除目录下所有文件(注意完整复制,不要漏了那个点)
rm -rf ./*

# 安装正确的源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

# 生成缓存
yum makecache

启动测试: #

#查看 docker 是否安装成功,和版本

[root@izwz91mv6i3x6k12jjqh60z ~]# docker -v

Docker version 20.10.6, build 370c289

#启动 docker
[root@izwz91mv6i3x6k12jjqh60z ~]# systemctl start docker

#运行测试
[root@izwz91mv6i3x6k12jjqh60z ~]# docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
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/

#查看下载的 hello-world 镜像
[root@izwz91mv6i3x6k12jjqh60z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   7 weeks ago   13.3kB

卸载Docker: #

#卸载
yum remove docker-ce docker-ce-cli containerd.io

#删除文件  
rm -rf /var/lib/docker

#/var/lib/docker  是docker的默认工作路径

配置阿里云镜像仓库地址 #

参考教程:https://www.cnblogs.com/allenjing/p/12575972.html

登陆阿里云,找到如下选项:

image-20210428092132408.png

image-20210428092924853.png

sudo mkdir -p /etc/docker

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

sudo systemctl daemon-reload

sudo systemctl restart docker

基本原理 #

helloworld的运行流程: #

image-20210428121216893.png

image-20210428121520142.png

底层原理: #

Docker工作原理:Docker是一个 CS结构 的系统,Docker 的守护进程运行在主机上,通过 Socket从客户端访问。

DockerServer 接收到 DokcerClient 的指令,就会执行这个命令。

image-20210428122527218.png

Docker为什么比VM快? #

  1. Docker 有着比虚拟机更少的抽象层
  2. Docker 利用的是宿主机的内核,而 VM 需要的是 Guest OS
  3. 新建一个容器时,Docker 不需要像虚拟机一样重新加载一个操作系统的内核。

image-20210428122751694.png

image-20210428123110597.png