如何在 CentOS 7 上安装 Docker


Docker 是一个开源项目,它自动化了在软件容器内部署应用程序的过程。容器允许开发者打包所有项目资源,例如库、依赖项、资产等。Docker 使用 **Go 语言**编写,由 **Dotcloud** 开发。它基本上是一个容器引擎,它使用 Linux 内核特性(如命名空间和控制组)在操作系统之上创建容器,并自动化容器上的应用程序部署。

安装 Docker

在 CentOS 上安装 Docker 之前,需要更新 CentOS 包。要更新包,请使用以下命令:

# yum -y update

示例输出应如下所示:

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* elrepo: mirrors.ircam.fr
* epel: mirror01.idc.hinet.net
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package autocorr-en.noarch 1:4.3.7.2-5.el7 will be updated
---> Package autocorr-en.noarch 1:4.3.7.2-5.el7_2.1 will be an update
---> Package avahi.x86_64 0:0.6.31-15.el7 will be updated
---> Package avahi.x86_64 0:0.6.31-15.el7_2.1 will be an update
---> Package avahi-autoipd.x86_64 0:0.6.31-15.el7 will be updated
---> Package avahi-autoipd.x86_64 0:0.6.31-15.el7_2.1 will be an update
............

要安装 Docker,请使用以下命令:

# yum -y install docker docker-registry

示例输出应如下所示:

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* elrepo: mirrors.ircam.fr
* epel: epel.mirror.net.in
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:1.8.2-10.el7.centos will be installed
--> Processing Dependency: docker-selinux >= 1.8.2-10.el7.centos for package: docker-1.8.2-10.el7.centos.x86_64
---> Package docker-registry.x86_64 0:0.9.1-7.el7 will be installed
--> Processing Dependency: python-sqlalchemy >= 0.9.8-1 for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-backports-lzma >= 0.0.2-8 for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-requests for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-gunicorn for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-gevent for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-flask for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: python-blinker for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: m2crypto for package: docker-registry-0.9.1-7.el7.x86_64
--> Processing Dependency: PyYAML for package: docker-registry-0.9.1-7.el7.x86_64
.......

启动 Docker

要在启动时启动 Docker,请使用以下命令:

# systemctl enable docker.service

示例输出应如下所示:

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

要启动 Docker,请使用以下命令:

# systemctl start docker.service

要查找 Docker 的状态,请使用以下命令:

# systemctl status docker.service

示例输出应如下所示:

docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-02-29 16:01:24 IST; 1min 18s ago
Docs: https://docs.docker.net.cn
Main PID: 27398 (docker)
CGroup: /system.slice/docker.service
└─27398 /usr/bin/docker daemon --selinux-enabled
Feb 29 16:00:56 linux docker[27398]: time="2016-02-29T16:00:56.139817103+05...e"
Feb 29 16:01:22 linux docker[27398]: time="2016-02-29T16:01:22.314033208+05...e"
Feb 29 16:01:22 linux docker[27398]: time="2016-02-29T16:01:22.314422184+05...e"
Feb 29 16:01:22 linux docker[27398]: time="2016-02-29T16:01:22.401448064+05...1"
Feb 29 16:01:22 linux docker[27398]: time="2016-02-29T16:01:22.647321427+05...e"
Feb 29 16:01:24 linux docker[27398]: time="2016-02-29T16:01:24.713353670+05...."
Feb 29 16:01:24 linux docker[27398]: time="2016-02-29T16:01:24.716886255+05...."
Feb 29 16:01:24 linux docker[27398]: time="2016-02-29T16:01:24.716900165+05...n"
Feb 29 16:01:24 linux docker[27398]: time="2016-02-29T16:01:24.716913621+05...os
Feb 29 16:01:24 linux systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
.........

下载 CentOS 的 Docker 容器

要下载 Docker 容器,请使用以下命令:

# docker pull centos

示例输出应如下所示:

Using default tag: latest
Trying to pull repository docker.io/library/centos ... latest: Pulling from library/centos
47d44cb6f252: Pull complete
f5079557f135: Pull complete
42c2aa730369: Pull complete
0e0217391d41: Pull complete
library/centos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:1272ae53bac7bf054dd209a0b4a8629bcc39526c2a767427c7639b630a224a9e
Status: Downloaded newer image for docker.io/centos:latest
.................

运行 Docker 容器

要运行 Docker,需要一个带有 **bash shell** 的基本 **CentOS** 容器。要运行 Docker,请使用以下命令:

# docker run -i -t centos /bin/bash

以上命令将用户带入 CentOS Docker 容器内的 **bash shell**。要退出 Docker,请使用 **Ctrl+p** 或 **Ctrl+q** 快捷键。有很多社区已经包含在 Docker 容器中,可以通过搜索找到。

例如,要搜索 **CentOS** 社区,请使用以下命令:

# docker search centos

示例输出应如下所示:

INDEX       NAME                                  DESCRIPTION                                   STARS      OFFICIAL  AUTOMATED
docker.io   docker.io/centos                      The official build of CentOS.                 1965        [OK]
docker.io   docker.io/jdeathe/centos-ssh          CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 15                    [OK]
docker.io   docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 9                     [OK]
docker.io   docker.io/blalor/centos               Bare-bones base CentOS 6.5 image 8                                  [OK]
docker.io   docker.io/nimmis/java-centos          This is docker images of CentOS 7 with dif... 7                     [OK]
docker.io   docker.io/torusware/speedus-centos    Always updated official CentOS docker imag... 7                     [OK]
docker.io   docker.io/centos/mariadb55-centos7 3                                                                      [OK]
docker.io   docker.io/nathonfowlie/centos-jre    Latest CentOS image with the JRE pre-insta...  3                     [OK]
docker.io   docker.io/nickistre/centos-lamp      LAMP on centos setup 3                                               [OK]
docker.io   docker.io/consol/sakuli-centos-xfce  Sakuli end-2-end testing and monitoring co...  2                     [OK]
docker.io   docker.io/darksheer/centos           Base Centos Image -- Updated hourly 1                                [OK]
docker.io   docker.io/layerworx/centos          CentOS container with etcd, etcdctl, confd...   1                     [OK]
docker.io   docker.io/lighthopper/orientdb-centos A Dockerfile for creating an OrientDB imag... 1                     [OK]
docker.io   docker.io/softvisio/centos Centos 1                                                                       [OK]
docker.io   docker.io/yajo/centos-epel          CentOS with EPEL and fully updated              1                     [OK]
docker.io   docker.io/blacklabelops/centos      CentOS Base Image! Built and Updates Daily!     0                     [OK]
docker.io   docker.io/dmglab/centos             CentOS with some extras - This is for the ...   0                     [OK]
docker.io   docker.io/ericuni/centos            centos dev                                      0                     [OK]
docker.io   docker.io/grayzone/centos           auto build for centos.                          0                     [OK]
docker.io   docker.io/insaneworks/centos        CentOS 6.5 x86_64 + @update                     0                     [OK]
docker.io   docker.io/januswel/centos           yum update-ed CentOS image                      0                     [OK]
docker.io   docker.io/jsmigel/centos-epel       Docker base image of CentOS w/ EPEL installed   0                     [OK]
docker.io   docker.io/lighthopper/openjdk-centos A Dockerfile for creating an OpenJDK image...  0                     [OK]
docker.io   docker.io/timhughes/centos          Centos with systemd installed and running       0                     [OK]
docker.io   docker.io/ustclug/centos            USTC centos                                     0                     [OK]

恭喜!现在您已经知道“如何在 CentOS 7 上安装 Docker”。我们将在下一篇文章中学习更多关于这类 Linux 命令的内容。继续关注!

更新于:2019年10月21日

404 次浏览

开启你的职业生涯

通过完成课程获得认证

开始学习
广告