欢迎光临
感受代码之美

CentOS安装docker ce(在线、离线安装两种方式)

docker.jpg

1.环境

  • CentOS Linux release 7.6.1810 (Core)

2.卸载旧版本

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

3.创建用户组

sudo groupadd docker

4.安装docker-ce

4.1 使用仓库安装

4.1.1 构建仓库

1)

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

2)

使用默认镜像:

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

或者使用阿里云镜像:

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3)

这一步可选(启用夜间或测试仓库):

sudo yum-config-manager --enable docker-ce-nightly

sudo yum-config-manager --enable docker-ce-test

sudo yum-config-manager --disable docker-ce-nightly

4.1.2 安装DOCKER CE

1)安装最新版

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

2)安装指定版本

yum list docker-ce --showduplicates | sort -r

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

4.1.3 启动Docker

sudo systemctl start docker

4.1.4 运行hello-world镜像,验证是否安装成功

[[email protected] ~]$ sudo systemctl start docker
[[email protected] ~]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
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/

此命令会下载一个测试镜像,并在容器内运行。当容器运行后,打印信息并退出。

4.1.5 升级Docker ce

参考上面使用仓库安装docker ce的方法,安装你想要安装的最新版本。

4.2 通过安装包安装

系统信息:

[[email protected] src]$  cat /proc/version
Linux version 3.10.0-957.12.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Tue May 14 21:24:32 UTC 2019
[[email protected] src]$ cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

4.2.1 下载

进入/usr/local/src目录,下载下列文件,地址https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

* containerd.io-1.2.5-3.1.el7.x86_64.rpm
* docker-ce-18.09.6-3.el7.x86_64.rpm
* docker-ce-cli-18.09.6-3.el7.x86_64.rpm
* docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm

4.2.2 安装


[[email protected] src]$ sudo yum -y install ./docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm [[email protected] src]$ sudo yum -y install ./containerd.io-1.2.5-3.1.el7.x86_64.rpm [[email protected] src]$ sudo yum -y install ./docker-ce-cli-18.09.7-3.el7.x86_64.rpm [[email protected] src]$ sudo yum -y install ./docker-ce-18.09.7-3.el7.x86_64.rpm Loaded plugins: fastestmirror, langpacks Examining ./docker-ce-18.09.7-3.el7.x86_64.rpm: 3:docker-ce-18.09.7-3.el7.x86_64 Marking ./docker-ce-18.09.7-3.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package docker-ce.x86_64 3:18.09.7-3.el7 will be obsoleting --> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.7-3.el7.x86_64 Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com ---> Package docker-ce-selinux.noarch 0:17.03.3.ce-1.el7 will be obsoleted --> Running transaction check ---> Package container-selinux.noarch 2:2.99-1.el7_6 will be installed --> Processing Dependency: selinux-policy-targeted >= 3.13.1-216.el7 for package: 2:container-selinux-2.99-1.el7_6.noarch --> Processing Dependency: selinux-policy-base >= 3.13.1-216.el7 for package: 2:container-selinux-2.99-1.el7_6.noarch --> Processing Dependency: selinux-policy >= 3.13.1-216.el7 for package: 2:container-selinux-2.99-1.el7_6.noarch --> Running transaction check ---> Package selinux-policy.noarch 0:3.13.1-192.el7 will be updated ---> Package selinux-policy.noarch 0:3.13.1-229.el7_6.12 will be an update --> Processing Dependency: policycoreutils >= 2.5-24 for package: selinux-policy-3.13.1-229.el7_6.12.noarch --> Processing Dependency: libsemanage >= 2.5-13 for package: selinux-policy-3.13.1-229.el7_6.12.noarch ---> Package selinux-policy-targeted.noarch 0:3.13.1-192.el7 will be updated ---> Package selinux-policy-targeted.noarch 0:3.13.1-229.el7_6.12 will be an update --> Running transaction check ---> Package libsemanage.x86_64 0:2.5-11.el7 will be updated --> Processing Dependency: libsemanage = 2.5-11.el7 for package: libsemanage-python-2.5-11.el7.x86_64 ---> Package libsemanage.x86_64 0:2.5-14.el7 will be an update --> Processing Dependency: libsepol >= 2.5-10 for package: libsemanage-2.5-14.el7.x86_64 --> Processing Dependency: libselinux >= 2.5-14 for package: libsemanage-2.5-14.el7.x86_64 ---> Package policycoreutils.x86_64 0:2.5-22.el7 will be updated --> Processing Dependency: policycoreutils = 2.5-22.el7 for package: policycoreutils-python-2.5-22.el7.x86_64 ---> Package policycoreutils.x86_64 0:2.5-29.el7_6.1 will be an update --> Processing Dependency: libselinux-utils >= 2.5-14 for package: policycoreutils-2.5-29.el7_6.1.x86_64 --> Running transaction check ---> Package libselinux.x86_64 0:2.5-12.el7 will be updated --> Processing Dependency: libselinux(x86-64) = 2.5-12.el7 for package: libselinux-python-2.5-12.el7.x86_64 ---> Package libselinux.x86_64 0:2.5-14.1.el7 will be an update ---> Package libselinux-utils.x86_64 0:2.5-12.el7 will be updated ---> Package libselinux-utils.x86_64 0:2.5-14.1.el7 will be an update ---> Package libsemanage-python.x86_64 0:2.5-11.el7 will be updated ---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be an update ---> Package libsepol.x86_64 0:2.5-8.1.el7 will be updated ---> Package libsepol.x86_64 0:2.5-10.el7 will be an update ---> Package policycoreutils-python.x86_64 0:2.5-22.el7 will be updated ---> Package policycoreutils-python.x86_64 0:2.5-29.el7_6.1 will be an update --> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-29.el7_6.1.x86_64 --> Running transaction check ---> Package libselinux-python.x86_64 0:2.5-12.el7 will be updated ---> Package libselinux-python.x86_64 0:2.5-14.1.el7 will be an update ---> Package setools-libs.x86_64 0:3.3.8-2.el7 will be updated ---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be an update --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================================================================================== Package Arch Version Repository Size =============================================================================================================================================================================================== Installing: docker-ce x86_64 3:18.09.7-3.el7 /docker-ce-18.09.7-3.el7.x86_64 81 M replacing docker-ce-selinux.noarch 17.03.3.ce-1.el7 Installing for dependencies: container-selinux noarch 2:2.99-1.el7_6 extras 39 k Updating for dependencies: libselinux x86_64 2.5-14.1.el7 base 162 k libselinux-python x86_64 2.5-14.1.el7 base 235 k libselinux-utils x86_64 2.5-14.1.el7 base 151 k libsemanage x86_64 2.5-14.el7 base 151 k libsemanage-python x86_64 2.5-14.el7 base 113 k libsepol x86_64 2.5-10.el7 base 297 k policycoreutils x86_64 2.5-29.el7_6.1 updates 916 k policycoreutils-python x86_64 2.5-29.el7_6.1 updates 456 k selinux-policy noarch 3.13.1-229.el7_6.12 updates 484 k selinux-policy-targeted noarch 3.13.1-229.el7_6.12 updates 6.9 M setools-libs x86_64 3.3.8-4.el7 base 620 k Transaction Summary =============================================================================================================================================================================================== Install 1 Package (+ 1 Dependent package) Upgrade ( 11 Dependent packages) Total size: 92 M Total download size: 39 k Downloading packages: container-selinux-2.99-1.el7_6.noarch.rpm | 39 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : libsepol-2.5-10.el7.x86_64 1/25 Updating : libselinux-2.5-14.1.el7.x86_64 2/25 Updating : libsemanage-2.5-14.el7.x86_64 3/25 Updating : libselinux-utils-2.5-14.1.el7.x86_64 4/25 Updating : policycoreutils-2.5-29.el7_6.1.x86_64 5/25 Updating : selinux-policy-3.13.1-229.el7_6.12.noarch 6/25 Updating : selinux-policy-targeted-3.13.1-229.el7_6.12.noarch 7/25 Updating : libsemanage-python-2.5-14.el7.x86_64 8/25 Updating : setools-libs-3.3.8-4.el7.x86_64 9/25 Updating : libselinux-python-2.5-14.1.el7.x86_64 10/25 Updating : policycoreutils-python-2.5-29.el7_6.1.x86_64 11/25 Installing : 2:container-selinux-2.99-1.el7_6.noarch 12/25 Installing : 3:docker-ce-18.09.7-3.el7.x86_64 13/25 Erasing : docker-ce-selinux-17.03.3.ce-1.el7.noarch 14/25 Cleanup : selinux-policy-targeted-3.13.1-192.el7.noarch 15/25 Cleanup : policycoreutils-python-2.5-22.el7.x86_64 16/25 Cleanup : selinux-policy-3.13.1-192.el7.noarch 17/25 Cleanup : policycoreutils-2.5-22.el7.x86_64 18/25 Cleanup : libselinux-utils-2.5-12.el7.x86_64 19/25 Cleanup : setools-libs-3.3.8-2.el7.x86_64 20/25 Cleanup : libselinux-python-2.5-12.el7.x86_64 21/25 Cleanup : libsemanage-python-2.5-11.el7.x86_64 22/25 Cleanup : libsemanage-2.5-11.el7.x86_64 23/25 Cleanup : libselinux-2.5-12.el7.x86_64 24/25 Cleanup : libsepol-2.5-8.1.el7.x86_64 25/25 Verifying : policycoreutils-2.5-29.el7_6.1.x86_64 1/25 Verifying : setools-libs-3.3.8-4.el7.x86_64 2/25 Verifying : selinux-policy-targeted-3.13.1-229.el7_6.12.noarch 3/25 Verifying : selinux-policy-3.13.1-229.el7_6.12.noarch 4/25 Verifying : 2:container-selinux-2.99-1.el7_6.noarch 5/25 Verifying : policycoreutils-python-2.5-29.el7_6.1.x86_64 6/25 Verifying : libsemanage-python-2.5-14.el7.x86_64 7/25 Verifying : libsemanage-2.5-14.el7.x86_64 8/25 Verifying : libsepol-2.5-10.el7.x86_64 9/25 Verifying : 3:docker-ce-18.09.7-3.el7.x86_64 10/25 Verifying : libselinux-python-2.5-14.1.el7.x86_64 11/25 Verifying : libselinux-utils-2.5-14.1.el7.x86_64 12/25 Verifying : libselinux-2.5-14.1.el7.x86_64 13/25 Verifying : libsemanage-python-2.5-11.el7.x86_64 14/25 Verifying : libsemanage-2.5-11.el7.x86_64 15/25 Verifying : libselinux-python-2.5-12.el7.x86_64 16/25 Verifying : setools-libs-3.3.8-2.el7.x86_64 17/25 Verifying : docker-ce-selinux-17.03.3.ce-1.el7.noarch 18/25 Verifying : policycoreutils-2.5-22.el7.x86_64 19/25 Verifying : policycoreutils-python-2.5-22.el7.x86_64 20/25 Verifying : libsepol-2.5-8.1.el7.x86_64 21/25 Verifying : selinux-policy-3.13.1-192.el7.noarch 22/25 Verifying : libselinux-2.5-12.el7.x86_64 23/25 Verifying : libselinux-utils-2.5-12.el7.x86_64 24/25 Verifying : selinux-policy-targeted-3.13.1-192.el7.noarch 25/25 Installed: docker-ce.x86_64 3:18.09.7-3.el7 Dependency Installed: container-selinux.noarch 2:2.99-1.el7_6 Dependency Updated: libselinux.x86_64 0:2.5-14.1.el7 libselinux-python.x86_64 0:2.5-14.1.el7 libselinux-utils.x86_64 0:2.5-14.1.el7 libsemanage.x86_64 0:2.5-14.el7 libsemanage-python.x86_64 0:2.5-14.el7 libsepol.x86_64 0:2.5-10.el7 policycoreutils.x86_64 0:2.5-29.el7_6.1 policycoreutils-python.x86_64 0:2.5-29.el7_6.1 selinux-policy.noarch 0:3.13.1-229.el7_6.12 selinux-policy-targeted.noarch 0:3.13.1-229.el7_6.12 setools-libs.x86_64 0:3.3.8-4.el7 Replaced: docker-ce-selinux.noarch 0:17.03.3.ce-1.el7 Complete!

从上面的执行日志看(下载了1个依赖,更新了11个依赖),就算是离线安装,也需要下载很多依赖,离线安装的前提是上面那些依赖你都具备。

4.2.3 启动

sudo systemctl start docker

4.2.4 运行hello-world镜像,验证是否安装成功

[[email protected] src]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
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/

4.2.5 升级Docker ce

需要下载上面安装文件的新版本,然后使用yum -y upgrade替代yum -y install命令即可完成升级。

5.添加用户到docker用户组

只有root用户和docker用户组的用户可以运行docker命令,所以上面的用户得sudo才能执行docker命令,我们可以将当前用户添加到docker用户组:

sudo usermod -aG docker $USER

sudo gpasswd -a $USER docker (推荐,这样不影响用户原有的用户组)

参考:

  1. Get Docker CE for CentOS
  2. Docker 安装 Tomcat
  3. Manage Docker as a non-root user
  4. Uninstall Docker CE
转载请注明来源:四个空格 » CentOS安装docker ce(在线、离线安装两种方式)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址