Docker的安装

安装docker(Linux)

  1. 环境准备:CentOS 7

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # 系统内核是3.10以上的
    [root@localhost ~]# uname -r
    3.10.0-957.el7.x86_64
    # 系统版本
    [root@localhost ~]# 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. CentOS安装docker官方文档安装介绍

    1. 卸载旧的docker

      1
      2
      3
      4
      5
      6
      7
      8
      yum remove docker \
      docker-client \
      docker-client-latest \
      docker-common \
      docker-latest \
      docker-latest-logrotate \
      docker-logrotate \
      docker-engine
  1. 安装docker依赖

    1
    yum install -y yum-utils
  1. 设置docker的yum源

    1
    2
    3
    4
    5
    6
    7
    # 这是国外的非常慢,不推荐使用
    yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

    # 国内阿里云镜像地址
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. 安装最新版的docker,docker-ce社区版 docker-ee企业版

    1
    2
    3
    4
    # 更新yum软件包索引
    yum makecache fast
    # 安装社区版docker相关
    yum install docker-ce docker-ce-cli containerd.io
  1. 指定版本安装docker

    1
    2
    3
    yum list docker-ce --showduplicates | sort -r
    yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
    yum install docker-ce-18.09.5-3.el7 docker-ce-cli-18.09.5-3.el7 containerd.io
  1. 启动docker

    1
    systemctl start docker
  1. 查看docker版本及信息

    1
    2
    docker version
    docker info
  1. 测试docker安装

    1
    docker run hello-world
  1. 关闭docker

    1
    systemctl stop docker
  1. 配置docker开机自启动

    1
    systemctl enable docker
  1. 建立docker组,并使用root用户

    1
    2
    3
    4
    5
    6
    # 创建docker用户组
    groupadd docker
    # 将当前用户加入docker组
    usermod -aG docker $USER
    # 重启docker服务
    systemctl restart docker

bash安装(通用所有平台)

  • 在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本,CentOS 系统上可以使用这套脚本安装,另外可以通过 --mirror 选项使用国内源进行安装:执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker 的稳定(stable)版本安装在系统中。

    1
    2
    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh --mirror Aliyun
  • 启动docker

    1
    2
    systemctl enable docker
    systemctl start docker
  • 创建docker用户组

    1
    groupadd docker
  • 将当前用户加入docker组

    1
    usermod -aG docker $USER
  • 测试docker安装是否正确

    1
    docker run hello-world

Docker 配置阿里镜像加速服务

docker 运行流程

image-20210206144026068

docker配置阿里云镜像加速

  1. 登录阿里云:https://www.aliyun.com

    • 登录成功后点击【控制台】 – 侧边栏,找到“容器镜像服务”,点击开通即可

    image-20210206144737426

  2. 镜像服务加速器

    image-20210206144811336

  3. 回到宿主机上,配置阿里云加速器

    1
    2
    3
    4
    5
    6
    7
    8
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
    "registry-mirrors": ["https://u25zlnlc.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
  4. 验证docker的镜像加速是否生效

    1
    2
    3
    4
    5
    6
    7
    8
    docker info
    ……
    Experimental: false
    Insecure Registries:
    127.0.0.0/8
    Registry Mirrors:
    https://u25zlnlc.mirror.aliyuncs.com/
    Live Restore Enabled: false

Docker的入门应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
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/

总结:

  1. docker run 镜像名,根据run后面的镜像运行一个容器
  2. 在运行之前在本地仓库中查找对应的镜像
    • 找到直接使用
    • 找不到,则会到远程仓库下载

image-20210206152138259