linux上安装Docker

Posted 程序猿001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux上安装Docker相关的知识,希望对你有一定的参考价值。

Docker的三大核心概念:镜像、容器、仓库

镜像:类似虚拟机的镜像、用俗话说就是安装文件。

容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例,

可以将其启动、开始、停止、删除、而这些容器都是相互隔离、互不可见的。

仓库:类似代码仓库,是Docker集中存放镜像文件的场所。

 

简单介绍一下在CentOS上安装Docker。

 

前置条件:

64-bit 系统

kernel 3.10+

 

1.检查内核版本,返回的值大于3.10即可。

  $ uname -r

技术分享图片

2.使用 sudo 或 root 权限的用户登入终端。

3.确保yum是最新的

  $ yum update

 

 

4.添加 yum 仓库

 

tee /etc/yum.repos.d/docker.repo <<-‘EOF‘  
[dockerrepo]  
name=Docker Repository  
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/  
enabled=1  
gpgcheck=1  
gpgkey=https://yum.dockerproject.org/gpg  
EOF  

 

 

 

技术分享图片

 

 

5.安装 Docker

  $ yum install -y docker-engine

安装成功后,使用docker version命令查看是否安装成功,安装成功后------如下图

技术分享图片

6.启动docker

  $systemctl start docker.service

7.验证安装是否成功(有client和service两部分表示docker安装启动都成功了)

使用docker version命令查看

技术分享图片

 

 

8.设置开机自启动

 $ sudo systemctl enable docker

 

 

到此为止docker就完全安装好了。

 

 

发现docker启动不了了,查看报错如下:

[[email protected] ~]# systemctl restart docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
[[email protected] ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2017-07-03 10:12:16 CST; 5s ago
     Docs: http://docs.docker.com
  Process: 16172 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 $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY (code=exited, status=1/FAILURE)
 Main PID: 16172 (code=exited, status=1/FAILURE)

Jul 03 10:12:15 ALY-HKC-PRO-001 systemd[1]: Starting Docker Application Container Engine...
Jul 03 10:12:15 ALY-HKC-PRO-001 dockerd-current[16172]: time="2017-07-03T10:12:15.812330534+08:00" level=info msg="libcontainerd: new containerd process, pid: 16180"
Jul 03 10:12:16 ALY-HKC-PRO-001 dockerd-current[16172]: time="2017-07-03T10:12:16.823237678+08:00" level=warning msg="devmapper: Usage of loopback devices is strongly discourage...v section."
Jul 03 10:12:16 ALY-HKC-PRO-001 dockerd-current[16172]: time="2017-07-03T10:12:16.838204058+08:00" level=error msg="[graphdriver] prior storage driver \"devicemapper\" failed: d...run failed"
Jul 03 10:12:16 ALY-HKC-PRO-001 dockerd-current[16172]: time="2017-07-03T10:12:16.838278116+08:00" level=fatal msg="Error starting daemon: error initializing graphdriver: devmap...run failed"
Jul 03 10:12:16 ALY-HKC-PRO-001 systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Jul 03 10:12:16 ALY-HKC-PRO-001 systemd[1]: Failed to start Docker Application Container Engine.
Jul 03 10:12:16 ALY-HKC-PRO-001 systemd[1]: Unit docker.service entered failed state.
Jul 03 10:12:16 ALY-HKC-PRO-001 systemd[1]: docker.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

 

我们可以看到此处它提示是Failed to start Docker Application Container Engine.

于是在网上找各种类似错误,有说是需要把firewalld.service及selinux关闭。呃,这个的话是能够导致docker启动不了,可博主这块儿不是因为这个原因导致。

博主第一时间能想到的只有重装docker,可反复删除几次后仍为解决问题。(期间以为会是kubernetes当时设置有关,于是将kubernetes的所有软件也删除,问题同样是没有解决)。

最后突然想起很早的时候有看到文章说docker的engine 基于Device Mapper提供的一种存储驱动,而它又依赖与 devicemapper。于是乎我将它给删除并重启服务,解决了docker无法正常启动的问题。

rm -rf /var/lib/docker/
# 添加如下内容
vim /etc/docker/daemon.json
{
    "graph": "/mnt/docker-data",
    "storage-driver": "overlay"
}

 

[[email protected] ~]# systemctl restart docker
[[email protected] ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-07-03 10:26:19 CST; 15s ago
     Docs: http://docs.docker.com
 Main PID: 17167 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─17167 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userlan...
           └─17175 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m -...

Jul 03 10:26:18 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:18.961963315+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jul 03 10:26:18 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:18.962211996+08:00" level=warning msg="mountpoint for pids not found"
Jul 03 10:26:18 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:18.962703210+08:00" level=info msg="Loading containers: start."
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: .........time="2017-07-03T10:26:19.230715822+08:00" level=info msg="Firewalld running: false"
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:19.379894482+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.30...IP address"
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:19.435937423+08:00" level=info msg="Loading containers: done."
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:19.436016788+08:00" level=info msg="Daemon has completed initialization"
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:19.436052792+08:00" level=info msg="Docker daemon" commit="1398f24/1.12.6" graphdriver=devicemappe...sion=1.12.6
Jul 03 10:26:19 ALY-HKC-PRO-001 dockerd-current[17167]: time="2017-07-03T10:26:19.441788942+08:00" level=info msg="API listen on /var/run/docker.sock"
Jul 03 10:26:19 ALY-HKC-PRO-001 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

 

 

 

第二种方法作为参考:

 

[[email protected] ~]# yum remove docker
[[email protected] ~]# yum remove docker-selinux
 
[[email protected] yum.repos.d]# vi  /etc/yum.repos.d/docker.repo
 
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
 
[[email protected] yum.repos.d]# yum install docker-engine
 
已安装:
  docker-engine.x86_64 0:1.10.2-1.el7.centos                                    
 
作为依赖被安装:
  docker-engine-selinux.noarch 0:1.10.2-1.el7.centos   
 
[[email protected] ~]# service docker start
Redirecting to /bin/systemctl start  docker.service
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 
[[email protected] yum.repos.d]# docker version
Client:
 Version:      1.10.2
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   c3959b1
 Built:        Mon Feb 22 16:16:33 2016
 OS/Arch:      linux/amd64
 
Server:
 Version:      1.10.2
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   c3959b1
 Built:        Mon Feb 22 16:16:33 2016
 OS/Arch:      linux/amd64
[[email protected] yum.repos.d]# 

 

 

 

 

 

 

 

 

 

 

[[email protected] ~]# yum remove docker
[[email protected] ~]# yum remove docker-selinux
 
[[email protected] yum.repos.d]# vi  /etc/yum.repos.d/docker.repo
 
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
 
[[email protected] yum.repos.d]# yum install docker-engine
 
已安装:
  docker-engine.x86_64 0:1.10.2-1.el7.centos                                    
 
作为依赖被安装:
  docker-engine-selinux.noarch 0:1.10.2-1.el7.centos   
 
[[email protected] ~]# service docker start
Redirecting to /bin/systemctl start  docker.service
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 
[[email protected] yum.repos.d]# docker version
Client:
 Version:      1.10.2
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   c3959b1
 Built:        Mon Feb 22 16:16:33 2016
 OS/Arch:      linux/amd64
 
Server:
 Version:      1.10.2
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   c3959b1
 Built:        Mon Feb 22 16:16:33 2016
 OS/Arch:      linux/amd64
[[email protected] yum.repos.d]# 

 

以上是关于linux上安装Docker的主要内容,如果未能解决你的问题,请参考以下文章

linux上安装Docker(非常简单的安装方法)

linux上安装Docker (非常简单的安装方法) 2019

linux上Docker安装gogs私服

linux_docker入门

在 Ubuntu16.04上安装并使用Docker

Linux上安装Docker