Docker是一个开放源代码软件项目,让应用程序布署在软件容器下的工作可以自动化进行,借此在Linux操作系统上,提供一个额外的软件抽象层,以及操作系统层虚拟化的自动管理机制。Docker利用Linux核心中的资源分脱机制,例如cgroups,以及Linux核心名字空间(name space),来创建独立的软件容器(containers)。这可以在单一Linux实体下运作,避免引导一个虚拟机造成的额外负担。Linux核心对名字空间的支持完全隔离了工作环境中应用程序的视野,包括进程树、网络、用户ID与挂载文件系统,而核心的cgroup提供资源隔离,包括CPU、内存、block I/O与网络 —— [ 维基百科 ]


前言

本教程适用于CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上

方法一(推荐直接方法二)

1、更新yum源

yum update -y

2、添加docker的yum源

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

3、安装docker

yum install docker-engine -y

4、设置为系统开机自动启动服务

systemctl enable docker.service

5、启动服务

systemctl start docker

方法二

1、安装依赖

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

2、配置源

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

以上命令会添加稳定版本的 docker ce yum 源,从 docker 17.06 开始,edge test 版本的 yum 源也会包含稳定版本的 docker ce

  • 如果需要最新版本的 Docker CE 请使用以下命令,默认不启用(可不选)
yum-config-manager --enable docker-ce-edge
yum-config-manager --enable docker-ce-test
  • 如果想关闭edge和test的repository,可以执行下面的命令(可不选)
yum-config-manager --disable docker-ce-edge
yum-config-manager --disable docker-ce-test

3、安装

  • 默认版本安装

    yum install docker-ce -y
    
  • 指定版本安装

在生产系统上,您应该安装特定版本的docker,而不是始终使用最新版本

# 列出 Docker 的版本
yum list docker-ce --showduplicates|sort -r
# 安装指定的版本
# yum install docker-ce-<VERSION>
yum install docker-ce-18.03.1.ce -y

4、查看进程

systemctl list-unit-files|grep docker

5、开机自启

systemctl enable docker

6、启动服务

systemctl start docker

使用 DaoCloud 加速器获取 Docker 官方镜像

https://www.daocloud.io/mirror#accelerator-doc

因为 docker 默认镜像是国外,下载速度可能会比较慢,如果需要登录请先注册,然后再点击,获取加速器代码,或者使用本文最后的阿里云加速源

加速器安装后,重启 docker

systemctl restart docker

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

docker run hello-world

输出以下信息

[root@service ~]# 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

至此,我们已成功安装并运行起 Docker 容器


附:

安装docker-ce-17.03.2.ce版本时问题

(1)

[root@service ~]# yum install docker-ce-17.03.2.ce
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.2.ce-1.el7.centos will be installed
--> Processing Dependency: docker-ce-selinux >= 17.03.2.ce-1.el7.centos for package: docker-ce-17.03.2.ce-1.el7.centos.x86_64
Package docker-ce-selinux is obsoleted by docker-ce, but obsoleting package does not provide for requirements
--> Processing Dependency: libcgroup for package: docker-ce-17.03.2.ce-1.el7.centos.x86_64
--> Processing Dependency: libltdl.so.7()(64bit) for package: docker-ce-17.03.2.ce-1.el7.centos.x86_64
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.2.ce-1.el7.centos will be installed
--> Processing Dependency: docker-ce-selinux >= 17.03.2.ce-1.el7.centos for package: docker-ce-17.03.2.ce-1.el7.centos.x86_64
Package docker-ce-selinux is obsoleted by docker-ce, but obsoleting package does not provide for requirements
---> Package libcgroup.x86_64 0:0.41-15.el7 will be installed
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
--> Finished Dependency Resolution
Error: Package: docker-ce-17.03.2.ce-1.el7.centos.x86_64 (docker-ce-stable)
           Requires: docker-ce-selinux >= 17.03.2.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.0.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.1.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.2.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.3.ce-1.el7.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.3.ce-1.el7
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

解决办法:

要先安装docker-ce-selinux-17.03.2.ce,再安装docker-ce-17.03.2.ce

yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm

(2)

[root@service ~]# yum install docker-ce-17.03.2.ce -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:17.03.2.ce-1.el7.centos will be installed
--> Processing Dependency: libltdl.so.7()(64bit) for package: docker-ce-17.03.2.ce-1.el7.centos.x86_64
--> Running transaction check
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================================================================================================
 Package                                               Arch                                            Version                                                           Repository                                                 Size
=========================================================================================================================================================================================================================================
Installing:
 docker-ce                                             x86_64                                          17.03.2.ce-1.el7.centos                                           docker-ce-stable                                           19 M
Installing for dependencies:
 libtool-ltdl                                          x86_64                                          2.4.2-22.el7_3                                                    base                                                       49 k

Transaction Summary
=========================================================================================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total size: 19 M
Total download size: 19 M
Installed size: 19 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
docker-ce-17.03.2.ce-1.el7.cen FAILED                                          
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm: [Errno 14] curl#6 - "Could not resolve host: download.docker.com; Unknown error"       ]  0.0 B/s |    0 B  --:--:-- ETA 
Trying other mirror.


Error downloading packages:
  docker-ce-17.03.2.ce-1.el7.centos.x86_64: [Errno 256] No more mirrors to try.

docker-ce-17.03.2.ce-1.el7.centos.x86_64这个rpm包下载不下来,所以只能手动下载安装了

# 下载docker-ce-17.03.2.ce-1.el7.centos.x86_64 rpm包
wget http://soft.ryana.cn/%E5%BC%80%E5%8F%91%E7%9B%B8%E5%85%B3/Docker/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

# 安装
yum install docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

阿里云加速源

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

results matching ""

    No results matching ""