CentOS Docker 安装

前提条件

目前,CentOS 仅发行版本中的内核支持 Docker

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。


查看系统版本

[root@localhost ~]# lsb_release -a
-bash: lsb_release: command not found

(说明没有安装lsb_release。)


安装lsb_release

[root@localhost ~]# yum install -y redhat-lsb
Loaded plugins:fastestmirror
Loading mirrorspeeds from cached hostfile
 * base:mirrors.cn99.com
 * extras:mirrors.cn99.com
 * updates:mirrors.cn99.com
ResolvingDependencies
--> Runningtransaction check
---> Packageredhat-lsb.x86_64 0:4.1-27.el7.centos.1 will be installed
-->Processing Dependency: redhat-lsb-printing = 4.1-27.el7.centos.1 for package:redhat-lsb-4.1-27.el7.centos.1.x86_64
-->Processing Dependency: redhat-lsb-languages = 4.1-27.el7.centos.1 for package:redhat-lsb-4.1-27.el7.centos.1.x86_64
-->Processing Dependency: redhat-lsb-desktop(x86-64) = 4.1-27.el7.centos.1 forpackage: redhat-lsb-4.1-27.el7.centos.1.x86_64
-->Processing Dependency: redhat-lsb-cxx(x86-64) = 4.1-27.el7.centos.1 forpackage: redhat-lsb-4.1-27.el7.centos.1.x86_64
-->Processing Dependency: redhat-lsb-core(x86-64) = 4.1-27.el7.centos.1 forpackage: redhat-lsb-4.1-27.el7.centos.1.x86_64
--> Runningtransaction check
---> Packageredhat-lsb-core.x86_64 0:4.1-27.el7.centos.1 will be installed
......略过中间内容......
 qt-x11.x86_64 1:4.8.5-15.el7_4                                                qt3.x86_640:3.3.8b-51.el7                                                     
 redhat-lsb-core.x86_64 0:4.1-27.el7.centos.1                                  redhat-lsb-cxx.x86_64 0:4.1-27.el7.centos.1                                    
 redhat-lsb-desktop.x86_64 0:4.1-27.el7.centos.1                               redhat-lsb-languages.x86_64 0:4.1-27.el7.centos.1                              
 redhat-lsb-printing.x86_64 0:4.1-27.el7.centos.1                              redhat-lsb-submod-multimedia.x86_64 0:4.1-27.el7.centos.1                       
 redhat-lsb-submod-security.x86_64 0:4.1-27.el7.centos.1                       spax.x86_64 0:1.5.2-13.el7                                                     
 systemtap-sdt-devel.x86_64 0:3.1-5.el7_4                                      time.x86_64 0:1.7-45.el7                                                      
 urw-fonts.noarch 0:2.4-16.el7                                                 xdg-utils.noarch0:1.1.0-0.17.20120809git.el7                                  
 xorg-x11-font-utils.x86_64 1:7.5-20.el7                                      


DependencyUpdated:
 cups-libs.x86_64 1:1.6.3-29.el7           glibc.x86_64 0:2.17-196.el7_4.2       glibc-common.x86_640:2.17-196.el7_4.2       libdb.x86_640:5.3.21-21.el7_4      
 libdb-utils.x86_64 0:5.3.21-21.el7_4       openssl.x86_641:1.0.2k-8.el7         openssl-libs.x86_641:1.0.2k-8.el7          


Complete!

安装完成!

再次查看:

[root@localhost ~]#lsb_release -a
LSB Version:   :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linuxrelease 7.3.1611 (Core) 
Release:       7.3.1611

Codename:      Core


查看内核版本

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker

通过 uname -r 命令查看你当前的内核版本

[root@localhost ~]#uname -r

3.10.0-514.el7.x86_64


查看docker安装包

[root@localhost ~]#yum list docker

Loaded plugins: fastestmirror
base                                                                                                                                   | 3.6 kB  00:00:00     
extras                                                                                                                                 | 3.4 kB  00:00:00     
updates                                                                                                                                | 3.4 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                                           | 156 kB  00:00:01     
(2/4): extras/7/x86_64/primary_db                                                                                                       | 185 kB  00:00:01     
(3/4): base/7/x86_64/primary_db                                                                                                         | 5.7 MB  00:00:13     
(4/4): updates/7/x86_64/primary_db                                                                                                      | 6.9 MB  00:00:14     
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
Available Packages
docker.x86_64                                                     2:1.13.1-53.git774336d.el7.centos                                                     extras

 

使用 yum 安装(CentOS 7下)

Docker 软件包和依赖包已经包含在默认的 CentOS-Extras 软件源里,安装命令如下:

[root@localhost ~]# yum -y installdocker
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-53.git774336d.el7.centos will beinstalled
--> Processing Dependency: docker-common = 2:1.13.1-53.git774336d.el7.centosfor package: 2:docker-1.13.1-53.git774336d.el7.centos.x86_64
--> Processing Dependency: docker-client = 2:1.13.1-53.git774336d.el7.centosfor package: 2:docker-1.13.1-53.git774336d.el7.centos.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package:2:docker-1.13.1-53.git774336d.el7.centos.x86_64
--> Running transaction check
---> Package docker-client.x86_64 2:1.13.1-53.git774336d.el7.centos will beinstalled
---> Package docker-common.x86_64 2:1.13.1-53.git774336d.el7.centos will beinstalled

--> Processing Dependency: skopeo-containers >= 1:0.1.26-2 forpackage: 2:docker-common-1.13.1-53.git774336d.el7.centos.x86_64

......略过中间内容......

Installed:
  docker.x86_64 2:1.13.1-53.git774336d.el7.centos                                                                                                              


Dependency Installed:
  audit-libs-python.x86_64 0:2.7.6-3.el7                                         checkpolicy.x86_64 0:2.5-4.el7                                                
  container-selinux.noarch 2:2.42-1.gitad8f0f7.el7                               container-storage-setup.noarch0:0.8.0-3.git1d27ecf.el7                         
  docker-client.x86_64 2:1.13.1-53.git774336d.el7.centos                         docker-common.x86_64 2:1.13.1-53.git774336d.el7.centos                         
  libcgroup.x86_64 0:0.41-13.el7                                                 libseccomp.x86_640:2.3.1-3.el7                                                
  libsemanage-python.x86_64 0:2.5-8.el7                                          oci-register-machine.x86_641:0-6.git2b44233.el7                                
  oci-systemd-hook.x86_64 1:0.1.15-2.gitc04483d.el7                              oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7                                     
  policycoreutils-python.x86_64 0:2.5-17.1.el7                                   python-IPy.noarch 0:0.75-6.el7                                                
  setools-libs.x86_64 0:3.3.8-1.1.el7                                            skopeo-containers.x86_641:0.1.28-1.git0270e56.el7                              
  yajl.x86_64 0:2.0.4-4.el7                                                     


Dependency Updated:
  audit.x86_64 0:2.7.6-3.el7        audit-libs.x86_64 0:2.7.6-3.el7        libsemanage.x86_64 0:2.5-8.el7        policycoreutils.x86_64 0:2.5-17.1.el7        



Complete!

安装完成!


查看docker状态

[root@localhost ~]# service docker status
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled;vendor preset: disabled)
   Active: inactive (dead)

    Docs: http://docs.docker.com


启动 Docker 后台服务

[root@localhost ~]# service docker start
Redirecting to /bin/systemctl start  docker.service
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
[root@localhost ~]# 
[root@localhost ~]# service docker status
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2018-04-18 21:24:10 EDT; 13s ago
     Docs: http://docs.docker.com
  Process: 21623 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)
 Main PID: 21623 (code=exited, status=1/FAILURE)


Apr 18 21:24:08 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
Apr 18 21:24:08 localhost.localdomain dockerd-current[21623]: time="2018-04-18T21:24:08.846636996-04:00" level=warning msg="could not change group /var/r...t found"
Apr 18 21:24:08 localhost.localdomain dockerd-current[21623]: time="2018-04-18T21:24:08.855702265-04:00" level=info msg="libcontainerd: new containerd pr...: 21628"
Apr 18 21:24:10 localhost.localdomain dockerd-current[21623]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kern...d=false)
Apr 18 21:24:10 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Apr 18 21:24:10 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
Apr 18 21:24:10 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
Apr 18 21:24:10 localhost.localdomain systemd[1]: docker.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

启动失败了,加上-l参数看看详细信息:

[root@localhost ~]# systemctl status docker.service -l
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2018-04-18 21:24:10 EDT; 7min ago
     Docs: http://docs.docker.com
  Process: 21623 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)
 Main PID: 21623 (code=exited, status=1/FAILURE)


Apr 18 21:24:08 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
Apr 18 21:24:08 localhost.localdomain dockerd-current[21623]: time="2018-04-18T21:24:08.846636996-04:00" level=warning msg="could not change group /var/run/docker.sock to docker: group docker not found"
Apr 18 21:24:08 localhost.localdomain dockerd-current[21623]: time="2018-04-18T21:24:08.855702265-04:00" level=info msg="libcontainerd: new containerd process, pid: 21628"
Apr 18 21:24:10 localhost.localdomain dockerd-current[21623]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer kernel or disable selinux in docker (--selinux-enabled=false)
Apr 18 21:24:10 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Apr 18 21:24:10 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
Apr 18 21:24:10 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
Apr 18 21:24:10 localhost.localdomain systemd[1]: docker.service failed.

意思是说:此linux的内核中的SELinux不支持 overlay2 graph driver ,解决方法有两个,要么启动一个新内核,要么就在docker里禁用selinux,--selinux-enabled=false。


在docker里禁用selinux

[root@localhost ~]# sed  -i 's/--selinux-enabled/--selinux-enabled=false/' /etc/sysconfig/docker 
[root@localhost ~]# 
[root@localhost ~]# cat /etc/sysconfig/docker
# /etc/sysconfig/docker


# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

......


再次启动Docker 后台服务

[root@localhost ~]# service docker start              
Redirecting to /bin/systemctl start  docker.service

(看到以上的输出表示docker启动成功。)

此时docker的状态
[root@localhost ~]# service docker status                                                   
Redirecting to /bin/systemctl status  docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-04-18 21:45:24 EDT; 9s ago
     Docs: http://docs.docker.com
 Main PID: 21826 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─21826 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgr...
           └─21832 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --st...


Apr 18 21:45:23 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:23.754834040-04:00" level=info msg="Firewalld running: true"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.075648535-04:00" level=info msg="Default bridge (docker0) is assi...address"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.430279283-04:00" level=info msg="Loading containers: done."
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.435719771-04:00" level=warning msg="Not using native diff for ove... to fix"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.482004848-04:00" level=warning msg="failed to retrieve docker-run...2-dev\n"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.482080718-04:00" level=warning msg="failed to retrieve docker-init version"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.484338702-04:00" level=info msg="Daemon has completed initialization"
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.484374551-04:00" level=info msg="Docker daemon" commit="774336d/1...n=1.13.1
Apr 18 21:45:24 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Apr 18 21:45:24 localhost.localdomain dockerd-current[21826]: time="2018-04-18T21:45:24.495339351-04:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.

此时,docker已正常运行。


测试运行hello-world

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/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/engine/userguide/


以上是通过yum安装docker的过程,下面简介用脚本安装docker:

使用脚本安装 Docker

1、使用 sudo 或 root 权限登录 Centos。

2、确保 yum 包更新到最新。

$ sudo yum update

3、执行 Docker 安装脚本。

$ curl -fsSL https://get.docker.com/ | sh

执行这个脚本会添加 docker.repo 源并安装 Docker。

4、启动 Docker 进程。

$ sudo service docker start

5、验证 docker 是否安装成功并在容器中执行一个测试的镜像。

$ sudo docker run hello-world

到此,docker 在 CentOS 系统的安装完成。




你可能感兴趣的:(docker)