centos 7 安装 Docker Engine-CentOS 社区版

获取Docker Engine-CentOS社区: https://docs.docker.com/install/linux/docker-ce/centos/

1.操作系统要求

1.1 要安装 Docker Engine-Community ,您需要一个 CentOS 7 的维护版本.不支持或未测试存档版本.

1.2 该 centos-extras 库必须启用.默认情况下,此存储库是启用的,但是如果已禁用它,则需要 重新启用它.

1.3 overlay2 建议使用存储驱动程序.

< centos-extras >
CentOS Extras-在 CentOS 5 和 6 中,为CentOS提供附加功能而不破坏上游兼容性或更新基本组件的软件包,但未经上游测试或上游产品中没有提供. 在 CentOS 7 中,这是一个上游存储库,以及其他CentOS软件包.CentOS 开发团队已经测试了该存储库中的每个项目,并且它们都与 CentOS 一起使用. 该存储库随CentOS一起提供,并且默认情况下处于启用状态.

 

------------------------------------------------------------------------------------------
开启 CentOS Extra
路径 /etc/yum.repos.d
文件 CentOS-Base.repo
[extras] //extras 模块
enabled=0 //编辑把 "0" 修改为 "1" ,没有enabled=0或1,建议添加 enabled=1 启动 extras 软件模块仓库
#软件仓库被配置成 enabled=0 时,yum 在安装或升级软件包时不会将该仓库做为软件包提供源.
------------------------------------------------------------------------------------------
[root@localhost yum.repos.d]# (cat -n CentOS-Base.repo;pwd)
     1  # CentOS-Base.repo
     2  #
     3  # The mirror system uses the connecting IP address of the client and the
     4  # update status of each mirror to pick mirrors that are updated to and
     5  # geographically close to the client.  You should use this for CentOS updates
     6  # unless you are manually picking other mirrors.
     7  #
     8  # If the mirrorlist= does not work for you, as a fall back you can try the 
     9  # remarked out baseurl= line instead.
    10  #
    11  #
    12
    13  [base]
    14  name=CentOS-$releasever - Base
    15  mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    16  #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
    17  gpgcheck=1
    18  gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    19
    20  #released updates 
    21  [updates]
    22  name=CentOS-$releasever - Updates
    23  mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    24  #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
    25  gpgcheck=1
    26  gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    27
    28  #additional packages that may be useful
    29  [extras]
    30  name=CentOS-$releasever - Extras
    31  mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    32  #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
    33  gpgcheck=1
    34  enabled=1
    35  gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    36
    37  #additional packages that extend functionality of existing packages
    38  [centosplus]
    39  name=CentOS-$releasever - Plus
    40  mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    41  #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
    42  gpgcheck=1
    43  enabled=0
    44  gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    45
/etc/yum.repos.d

 2.卸载旧版本

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

较旧的Docker版本称为docker或docker-engine.如果已安装这些程序,请卸载它们以及相关的依赖项.

[root@localhost ~]# sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine

3.使用存储库安装

在新主机上首次安装Docker Engine-Community之前,需要设置Docker存储库.之后,您可以从存储库安装和更新Docker.

设置存储库

3.1 安装所需的软件包.yum-utils提供了yum-config-manager 效用,并device-mapper-persistent-data和lvm2由需要 devicemapper存储驱动程序.

sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
[root@localhost ~]# sudo yum install -y yum-utils \
> device-mapper-persistent-data \
> lvm2

3.2 使用以下命令来设置稳定的存储库.

sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost ~]# sudo yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo

 4.安装 DOCKER ENGINE-CentOS 社区版

安装最新版本的Docker Engine-Community和containerd,或者转到下一步安装特定版本:

sudo yum install -y docker-ce docker-ce-cli containerd.io
[root@localhost ~]# sudo yum install -y docker-ce docker-ce-cli containerd.io 

如果提示您接受GPG密钥,请验证指纹是否匹配 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35,如果是,则接受它.

5.有多个Docker存储库吗?

如果启用了多个Docker存储库,则在未在yum installor yum update命令中指定版本的情况下进行安装或更新将始终安装可能的最高版本,这可能不适合您的稳定性需求.

Docker已安装但尚未启动.docker创建该组,但没有用户添加到该组.

要安装特定版本的Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:

一种.列出并排序您存储库中可用的版本.此示例按版本号(从高到低)对结果进行排序,并被截断:

yum list docker-ce --showduplicates | sort -r
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
 * extras: mirrors.aliyun.com
 * epel: hkg.mirror.rackspace.com
docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.3-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
 * base: mirrors.aliyun.com
Available Packages

返回的列表取决于启用的存储库,并且特定于您的CentOS版本(.el7在此示例中以后缀表示)

通过其完全合格的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔.例如,docker-ce-18.09.1

sudo yum install -y docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

Docker已安装但尚未启动.

6.启动Docker

[root@localhost ~]# sudo systemctl start docker 

7.检查Docker进程

[root@localhost ~]# pgrep -l docker
7286 dockerd

8.通过运行hello-world 映像来验证是否正确安装了Docker Engine-Community.

[root@localhost ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

运行命令下载测试图像并在容器中运行。容器运行时,它会打印参考消息并退出.

Docker Engine-Community已安装并正在运行.

您需要使用sudo来运行Docker命令.继续进行Linux后安装,以允许非特权用户运行Docker命令以及其他可选配置步骤.

Docker创建该组,但没有用户添加到该组.

1.创建用户名

[root@localhost ~]# useradd wwweee000
[root@localhost ~]# passwd wwweee000
Changing password for user wwweee000.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.

2.创建docker用户组名

[root@localhost ~]# sudo groupadd docker
groupadd: group 'docker' already exists

3. 应用wwweee000用户加入docker用户组

[root@localhost ~]# sudo usermod -aG docker wwweee000

4. 切换或者退出当前账户再从新登入

su root             切换到root用户
su wwweee000        再切换到原来的应用用户以上配置才生效

[root@localhost ~]# vi /etc/sudoers
root    ALL=(ALL)         ALL
wwweee000    ALL=(ALL)         ALL
[root@localhost ~]# cat -n /etc/sudoers | grep "root\|wwweee000"
     2  ## the root user, without needing the root password.
    99  ## Allow root to run any commands anywhere 
   100  root    ALL=(ALL)       ALL
   101  wwweee000    ALL=(ALL)       ALL
   114  ## cdrom as root
[root@localhost ~]# chmod u+w /etc/sudoers
[root@localhost ~]# newgrp docker
[root@localhost ~]# sudo systemctl restart docker
[root@localhost ~]# su wwweee000
[wwweee000@localhost root]$ cd
[wwweee000@localhost ~]$ docker run hello-world

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/
[wwweee000@localhost ~]$ su root
Password: 
[root@localhost wwweee000]# cd
[root@localhost ~]# 

说明:
这里说下你可以sudoers添加下面四行中任意一条 

youuser ALL=(ALL) ALL 
%youuser ALL=(ALL) ALL 
youuser ALL=(ALL) NOPASSWD: ALL 
%youuser ALL=(ALL) NOPASSWD: ALL

第一行:允许用户youuser执行sudo命令(需要输入密码). 
第二行:允许用户组youuser里面的用户执行sudo命令(需要输入密码). 
第三行:允许用户youuser执行sudo命令,并且在执行的时候不输入密码. 
第四行:允许用户组youuser里面的用户执行sudo命令,并且在执行的时候不输入密码.

撤销sudoers文件写权限,命令: chmod u-w /etc/sudoers

 ----------------------------------------------------------------------------------------------------------------------------------------------------------

 ------------------------------------------------------------------安装完成  ---------------------------------------------------------------------------

 ----------------------------------------------------------------------------------------------------------------------------------------------------------

 ----------------------------------------------------------------------------------------------------------------------------------------------------------

10.从软件包安装(参考)

如果无法使用Docker的存储库安装Docker,则可以下载该.rpm发行版的 文件并手动安装.
每次升级Docker Engine-Community时,都需要下载一个新文件.

转到 https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ 并下载.rpm要安装的Docker版本的文件.

注意:要安装夜间 或测试(预发布)软件包,stable 请将上述URL中的单词更改为nightly或test. 了解每晚和测试频道.

安装 Docker Engine-Community ,将下面的路径更改为您下载Docker软件包的路径.

[root@localhost ~]# sudo yum install /path/to/package.rpm

Docker已安装但尚未启动.docker创建该组,但没有用户添加到该组.

启动Docker.

[root@localhost ~]# sudo systemctl start docker

通过运行hello-world 映像来验证是否正确安装了 Docker Engine-Community .

[root@localhost ~]# sudo docker run hello-world

运行命令下载测试图像并在容器中运行.容器运行时,它会打印参考消息并退出.

Docker Engine-Community已安装并正在运行.您需要使用sudo来运行Docker命令.
继续执行Linux的安装后步骤,以允许非特权用户运行Docker命令以及其他可选配置步骤.

升级DOCKER引擎-社区

要升级Docker Engine-Community,请下载更新的软件包文件,然后使用 代替,并指向新文件,重复 安装过程.

[root@localhost ~]# yum -y upgradeyum -y install

11.使用便捷脚本进行安装(参考)

Docker在get.docker.com 和test.docker.com上提供了方便脚本,用于将Docker Engine-Community的边缘版本和测试版本快速且非交互地安装到开发环境中.
脚本的源代码在 docker-install存储库中.
不建议在生产环境中使用这些脚本,在使用它们之前,您应该了解潜在的风险:

脚本需要运行root或具有sudo特权.因此,在运行脚本之前,应仔细检查和审核脚本.
这些脚本尝试检测Linux发行版和版本,并为您配置软件包管理系统.此外,脚本不允许您自定义任何安装参数.
从Docker的角度或您自己组织的准则和标准的角度来看,这可能导致不支持的配置.
这些脚本将安装软件包管理器的所有依赖项和建议,而无需进行确认.这可能会安装大量软件包,具体取决于主机的当前配置.
该脚本未提供用于指定要安装哪个版本的Docker的选项,而是安装了在“ edge”通道中发布的最新版本.
如果已使用其他机制将Docker安装在主机上,请不要使用便捷脚本.
本示例使用get.docker.com上的脚本在Linux上安装最新版本的Docker Engine-Community.
要安装最新的测试版本,请改用test.docker.com.在下面的每个命令,取代每次出现get用test.

警告:

在本地运行之前,请务必检查从Internet下载的脚本.

[root@localhost ~]# curl -fsSL https://get.docker.com -o get-docker.sh
[root@localhost ~]# sudo sh get-docker.sh

<output truncated>

如果要使用Docker作为非root用户,则现在应考虑使用类似以下方式将用户添加到“ docker”组:

[root@localhost ~]# sudo usermod -aG docker wwweee000

请记住注销并重新登录才能生效!

警告:

将用户添加到“docker”组后,他们可以运行容器,该容器可用于获取Docker主机上的根特权.

Docker Engine-CentOS社区已安装.它会自动在DEB基于发布的版本上启动.
在 RPM 基于发行版的系统上,您需要使用相应的systemctl或service命令手动启动它 .
如消息所示,默认情况下,非root用户无法运行Docker命令.

使用便捷脚本后升级DOCKER
如果使用便捷脚本安装了Docker ,则应直接使用包管理器升级 Docker.
重新运行便利程序脚本没有任何好处,并且如果尝试重新添加已经添加到主机的存储库,则可能导致问题.

卸载Docker Engine-社区
卸载Docker软件包:

[root@localhost ~]# sudo yum remove docker-ce

主机上的映像,容器,卷或自定义配置文件不会自动删除.要删除所有图像,容器和卷:

[root@localhost ~]# sudo rm -rf /var/lib/docker

您必须手动删除所有已编辑的配置文件.

猜你喜欢

转载自www.cnblogs.com/huixst/p/11646940.html
今日推荐