CentOS7下如何正确安装并启动Docker(图文详解)

  我使用了CentOS 7操作系统,可以非常容易地安装Docker环境。假设,下面我们都是用root用户进行操作,执行如下命令进行准备工作:

复制代码
yum install -y yum-utils
yum-config-manager \
    --add-repo \

https://docs.docker.com/engine/installation/linux/repo_files/centos/docker.repo

yum makecache fast
复制代码

  上面首先安装了yum-utils,它提供了yum-config-manager管理工具,然后安装了最新稳定版本的Repository文件,最后更新yum的package索引。

  执行如下命令:

sudo yum -y install docker-engine

  首次安装docker-engine,输出类似如下日志信息:

复制代码
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
Resolving Dependencies
--> Running transaction check
---> Package docker-engine.x86_64 0:1.13.1-1.el7.centos will be installed
--> Processing Dependency: docker-engine-selinux >= 1.13.1-1.el7.centos for package: docker-engine-1.13.1-1.el7.centos.x86_64
--> Running transaction check
---> Package docker-engine-selinux.noarch 0:1.13.1-1.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================
 Package                                                  Arch                                      Version                                                 Repository                                      Size
=================================================================================================================================================================================================================
Installing:
 docker-engine                                            x86_64                                    1.13.1-1.el7.centos                                     docker-main                                     19 M
Installing for dependencies:
 docker-engine-selinux                                    noarch                                    1.13.1-1.el7.centos                                     docker-main                                     28 k

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

Total download size: 19 M
Installed size: 65 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/docker-main/packages/docker-engine-selinux-1.13.1-1.el7.centos.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 2c52609d: NOKEY             ] 1.2 MB/s | 944 kB  00:00:14 ETA
Public key for docker-engine-selinux-1.13.1-1.el7.centos.noarch.rpm is not installed
(1/2): docker-engine-selinux-1.13.1-1.el7.centos.noarch.rpm                                                                                                                               |  28 kB  00:00:01     
(2/2): docker-engine-1.13.1-1.el7.centos.x86_64.rpm                                                                                                                                       |  19 MB  00:00:04     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                            4.5 MB/s |  19 MB  00:00:04     
Retrieving key from https://yum.dockerproject.org/gpg
Importing GPG key 0x2C52609D:
 Userid     : "Docker Release Tool (releasedocker) <[email protected]>"
 Fingerprint: 5811 8e89 f3a9 1289 7c07 0adb f762 2157 2c52 609d
 From       : https://yum.dockerproject.org/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : docker-engine-selinux-1.13.1-1.el7.centos.noarch                                                                                                                                              1/2
libsemanage.semanage_direct_install_info: Overriding docker module at lower priority 100 with module at priority 400.
restorecon:  lstat(/var/lib/docker) failed:  No such file or directory
warning: %post(docker-engine-selinux-1.13.1-1.el7.centos.noarch) scriptlet failed, exit status 255
Non-fatal POSTIN scriptlet failure in rpm package docker-engine-selinux-1.13.1-1.el7.centos.noarch
  Installing : docker-engine-1.13.1-1.el7.centos.x86_64                                                                                                                                                      2/2
  Verifying  : docker-engine-selinux-1.13.1-1.el7.centos.noarch                                                                                                                                              1/2
  Verifying  : docker-engine-1.13.1-1.el7.centos.x86_64                                                                                                                                                      2/2

Installed:
  docker-engine.x86_64 0:1.13.1-1.el7.centos                                                                                                                                                                     

Dependency Installed:
  docker-engine-selinux.noarch 0:1.13.1-1.el7.centos                                                                                                                                                             

Complete!
复制代码

  可见,Docker已经成功安装。

  下面,我们就可以启动Docker了,执行如下命令,启动Docker(Docker Engine):

systemctl start docker

  可以查看一下当前系统上的进程,执行ps -ef | grep docker确认Docker已经启动:

root       2717      1  8 21:52 ?        00:00:00 /usr/bin/dockerd
root       2723   2717  1 21:52 ?        00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root       2920   2645  0 21:52 pts/0    00:00:00 grep --color=auto docker

  下面,我们验证一下,Docker启动了,应该就可以在一个Container中运行一个准备好的应用,执行如下命令:

docker run hello-world
 

  基于一个名称为hello-world的Image,启动Container并运行它,启动过程如下所示:

 
复制代码
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
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.
 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

 
复制代码

  首先可以看到,因为本地没有下载过该Image,所以会先从Docker Hub上下载,对应的tag是latest。另外,也可以看到提示信息“Hello from Docker! ”,表示我们的环境配置没问题,可以启动Container运行应用程序。这里,还给出了运行我们这个名称为hello-world的示例Image在Container中运行过程中。

Docker的基本运行机制如下所示:

  1. Docker Client连接到Docker daemon
  2. Docker daemon从Docker Hub上下载名称为hello-world的Image
  3. Docker daemon基于这个Image创建了一个新的Container,并运行应用程序,输出“Hello from Docker!”
  4. Docker daemon将结果输出到Docker Client,也就是我们的终端上

现在,我们可能想知道hello-world这个Image是如何构建,才能够最终在我们的Docker Container中运行,请看下文。

猜你喜欢

转载自www.cnblogs.com/wangsongbai/p/9116255.html