Docker2.Linux安装docker

Docker官方帮助文档:https://docs.docker.com

服务器环境

查看服务器内核环境

# 表示系统内核是3.10以上
[haokai@localhost ~]$ uname -r
3.10.0-1160.el7.x86_64
[haokai@localhost ~]$

查看系统版本

[haokai@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"

查看帮助文档开始安装

ManualsDocker EngineInstall
进入Guides后>Get Docker>Docker for Linux>Installation per distro>Install on CentOS
在这里插入图片描述
在这里插入图片描述

安装步骤

1.Uninstall old versions:卸载旧版本

命令:

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 运行输出,别复制这里的命令,会多>
[haokai@localhost ~]$ sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
[sudo] password for haokai:
Loaded plugins: fastestmirror, langpacks
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


2.Installation methods:安装方法选择:Install using the repository(安装仓库的方式)

sudo yum install -y yum-utils
# 运行输出:Nothing to do,我之前安装过yum源,因此提示这个
[haokai@localhost ~]$ sudo yum install -y yum-utils
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
base                                                                                                              | 3.6 kB  00:00:00
docker-ce-stable                                                                                                  | 3.5 kB  00:00:00
extras                                                                                                            | 2.9 kB  00:00:00
updates                                                                                                           | 2.9 kB  00:00:00
(1/3): docker-ce-stable/7/x86_64/primary_db                                                                       |  67 kB  00:00:01
(2/3): extras/7/x86_64/primary_db                                                                                 | 243 kB  00:00:02
(3/3): updates/7/x86_64/primary_db                                                                                |  12 MB  00:00:57
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do

3.Install using the repository:设置镜像仓库(用国内,别用官方推荐)

默认:国外的太慢,不用

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

修改镜像仓库地址为:docker阿里云镜像地址

sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 运行输出
[haokai@localhost ~]$ sudo yum-config-manager \
>     --add-repo \
>     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[sudo] password for haokai:
Loaded plugins: fastestmirror, langpacks
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
[haokai@localhost ~]$

4.Install Docker Engine:安装docker

4.1安装前,更新一下yum源索引,这样可以保证安装的是最新版本

# 命令:安装前,更新一下yum源索引,这样可以保证安装的是最新版本
yum makecache fast
# 运行输出
[haokai@localhost ~]$ yum makecache fast
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.nju.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.ustc.edu.cn
base                                                                                                              | 3.6 kB  00:00:00
docker-ce-stable                                                                                                  | 3.5 kB  00:00:00
extras                                                                                                            | 2.9 kB  00:00:00
updates                                                                                                           | 2.9 kB  00:00:00
Metadata Cache Created
[haokai@localhost ~]$
# docker-ce:核心,分ce(社区版)、ee(企业版),正常ce就可以,官方推荐也是ce
# docker-ce-cli:docker客户端
# docker-ce-cli:docker容器
sudo yum install docker-ce docker-ce-cli containerd.io
# 运行输出
[haokai@localhost ~]$ sudo yum install docker-ce docker-ce-cli containerd.io
[sudo] password for haokai:
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package containerd.io.x86_64 0:1.4.11-3.1.el7 will be installed
---> Package docker-ce.x86_64 0:17.12.1.ce-1.el7.centos will be updated
---> Package docker-ce.x86_64 3:20.10.10-3.el7 will be an update
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-20.10.10-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:20.10.10-3.el7 will be installed
--> Processing Dependency: docker-scan-plugin(x86-64) for package: 1:docker-ce-cli-20.10.10-3.el7.x86_64
--> Running transaction check
---> Package docker-ce-rootless-extras.x86_64 0:20.10.10-3.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-20.10.10-3.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-20.10.10-3.el7.x86_64
---> Package docker-scan-plugin.x86_64 0:0.9.0-3.el7 will be installed
--> Running transaction check
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
---> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
 Package                                  Arch                  Version                            Repository                       Size
=========================================================================================================================================
Installing:
 containerd.io                            x86_64                1.4.11-3.1.el7                     docker-ce-stable                 28 M
 docker-ce-cli                            x86_64                1:20.10.10-3.el7                   docker-ce-stable                 29 M
Updating:
 docker-ce                                x86_64                3:20.10.10-3.el7                   docker-ce-stable                 23 M
Installing for dependencies:
 docker-ce-rootless-extras                x86_64                20.10.10-3.el7                     docker-ce-stable                8.0 M
 docker-scan-plugin                       x86_64                0.9.0-3.el7                        docker-ce-stable                3.7 M
 fuse-overlayfs                           x86_64                0.7.2-6.el7_8                      extras                           54 k
 fuse3-libs                               x86_64                3.6.1-4.el7                        extras                           82 k
 slirp4netns                              x86_64                0.4.3-4.el7_8                      extras                           81 k

Transaction Summary
=========================================================================================================================================
Install  2 Packages (+5 Dependent packages)
Upgrade  1 Package

Total download size: 92 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for docker-ce-stable
(1/8): docker-ce-20.10.10-3.el7.x86_64.rpm                                                                        |  23 MB  00:04:01
(2/8): containerd.io-1.4.11-3.1.el7.x86_64.rpm                                                                    |  28 MB  00:04:53
(3/8): docker-ce-rootless-extras-20.10.10-3.el7.x86_64.rpm                                                        | 8.0 MB  00:01:18
(4/8): fuse3-libs-3.6.1-4.el7.x86_64.rpm                                                                          |  82 kB  00:00:03
(5/8): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                                                                       |  81 kB  00:00:08
(6/8): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm                                                                    |  54 kB  00:00:15
(7/8): docker-scan-plugin-0.9.0-3.el7.x86_64.rpm                                                                  | 3.7 MB  00:00:38
(8/8): docker-ce-cli-20.10.10-3.el7.x86_64.rpm                                                                    |  29 MB  00:03:42
-----------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                    204 kB/s |  92 MB  00:07:43
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:docker-ce-cli-20.10.10-3.el7.x86_64                                                                                 1/9
  Installing : docker-scan-plugin-0.9.0-3.el7.x86_64                                                                                 2/9
  Installing : fuse3-libs-3.6.1-4.el7.x86_64                                                                                         3/9
  Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                   4/9
  Installing : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                      5/9
  Installing : containerd.io-1.4.11-3.1.el7.x86_64                                                                                   6/9
  Updating   : 3:docker-ce-20.10.10-3.el7.x86_64                                                                                     7/9
  Installing : docker-ce-rootless-extras-20.10.10-3.el7.x86_64                                                                       8/9
  Cleanup    : docker-ce-17.12.1.ce-1.el7.centos.x86_64                                                                              9/9
  Verifying  : docker-ce-rootless-extras-20.10.10-3.el7.x86_64                                                                       1/9
  Verifying  : docker-scan-plugin-0.9.0-3.el7.x86_64                                                                                 2/9
  Verifying  : 3:docker-ce-20.10.10-3.el7.x86_64                                                                                     3/9
  Verifying  : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                   4/9
  Verifying  : containerd.io-1.4.11-3.1.el7.x86_64                                                                                   5/9
  Verifying  : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                      6/9
  Verifying  : fuse3-libs-3.6.1-4.el7.x86_64                                                                                         7/9
  Verifying  : 1:docker-ce-cli-20.10.10-3.el7.x86_64                                                                                 8/9
  Verifying  : docker-ce-17.12.1.ce-1.el7.centos.x86_64                                                                              9/9

Installed:
  containerd.io.x86_64 0:1.4.11-3.1.el7                               docker-ce-cli.x86_64 1:20.10.10-3.el7

Dependency Installed:
  docker-ce-rootless-extras.x86_64 0:20.10.10-3.el7   docker-scan-plugin.x86_64 0:0.9.0-3.el7   fuse-overlayfs.x86_64 0:0.7.2-6.el7_8
  fuse3-libs.x86_64 0:3.6.1-4.el7                     slirp4netns.x86_64 0:0.4.3-4.el7_8

Updated:
  docker-ce.x86_64 3:20.10.10-3.el7

Complete!
[haokai@localhost ~]$

5.Start Docker:启动docker

sudo systemctl start docker

6.docker version:查看是否安装成功

# 查看docker版本信息
docker version
# 运行输出:普通管理员限制权限,sudo切换root查看version就行
[haokai@localhost ~]$ docker version
Client: Docker Engine - Community
 Version:           20.10.10
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        b485636
 Built:             Mon Oct 25 07:44:50 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied

# 普通管理员限制权限,sudo切换root查看version就行
[haokai@localhost ~]$ sudo docker version
Client: Docker Engine - Community
 Version:           20.10.10
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        b485636
 Built:             Mon Oct 25 07:44:50 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.10
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       e2f740d
  Built:            Mon Oct 25 07:43:13 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.11
  GitCommit:        5b46e404f6b9f661a205e28d59c982d3634148f8
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[haokai@localhost ~]$

7.hello-world:测试运行hello-world镜像

sudo docker run hello-world
# 运行输出:Hello from Docker!代表安装docker成功
[haokai@localhost ~]$ sudo docker run hello-world
[sudo] password for haokai:

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/

[haokai@localhost ~]$

8.docker images:查看下载的镜像

sudo docker images
[haokai@localhost ~]$ sudo docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
tomcatv2_haokai   latest    c8ed70599b25   10 months ago   654MB
tomcat            latest    6d15a1d68603   10 months ago   649MB
mysql             latest    ab2f358b8612   10 months ago   545MB
redis             latest    ef47f3b6dc11   10 months ago   104MB
hello-world       latest    bf756fb1ae65   22 months ago   13.3kB

9.Uninstall Docker Engine:卸载docker

卸载3核心

sudo yum remove docker-ce docker-ce-cli containerd.io

删除资源文件

 sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd

9.配置镜像加速器(Docker 镜像源):加速镜像下载

安装 Docker 软件后,您可以直接通过 docker pull 命令拉取镜像。如您未配置镜像加速源,直接拉取 DockerHub 中的镜像,通常下载速度会比较慢。
为此,配置 Docker 镜像源加速镜像下载。

9.1配置阿里云镜像加速器

配置阿里云镜像加速器(需要登录):https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

目录:容器镜像服务/镜像加速器

针对Docker客户端版本大于 1.10.0 的用户
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://h50qbl8u.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

9.2配置腾讯云镜像加速器

配置腾讯云镜像加速器:https://cloud.tencent.com/document/product/1207/45596?from=14588

适用于 Linux 操作系统实例:
执行以下命令,打开 /etc/docker/daemon.json 配置文件。注意这里需要先创建/etc下的docker的文件夹,否则会报错“/etc/docker/daemon.json“ E212: Can‘t open file for writing

vim /etc/docker/daemon.json

按 i 切换至编辑模式,添加以下内容,并保存。

{
   "registry-mirrors": [
       "https://mirror.ccs.tencentyun.com"
  ]
}

执行以下命令,重启 Docker 即可。示例命令以 CentOS 7 为例。

sudo systemctl restart docker

可能遇到的问题:“/etc/docker/daemon.json“ E212: Can‘t open file for writing
**原因分析:**一般都是因为一次创建了文件夹同时创建了文件导致,vi /etc/docker/demo.json
解决方案:
手动 先进入etc目录下:cd /etc/
手动创建文件夹docker:mkdir docker/
进入docker:cd docker/
再创建文件并进入编辑

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值