Docker安装

可以在任何操作系统上安装Docker,无论是Mac,Windows,Linux还是任何云服务器。Docker 引擎在Linux发行版上运行。 在这里,我们将以Linux Ubuntu Xenial-16.04 [LTS]作为演示安装Docker引擎的过程。

前提条件:

Docker需要两个重要的安装要求:

  • 它仅适用于64位Linux安装,注意:是64位的Linux系统。
  • 它需要Linux内核版本3.10或更高版本。

要查看当前的内核版本,请打开终端并键入uname -r命令以查看内核版本:

查看内核版本命令如下:

zyiz@ubuntu:~$ uname -r
4.4.0-31-generic
zyiz@ubuntu:~$

查看操作系统是32位还是64位:

zyiz@ubuntu:~$ sudo uname --m
[sudo] password for zyiz:
x86_64
zyiz@ubuntu:~$

更新apt源

按照以下说明更新apt源。

  1. 打开一个终端窗口。
  2. 使用sudo命令作为root用户登录。
  3. 更新包信息并安装CA证书。

命令:

zyiz@ubuntu:~$ sudo apt-get update
zyiz@ubuntu:~$ sudo apt-get install apt-transport-http ca-certificates

参见下面的屏幕截图 -

  1. 添加新的GPG密钥,使用以下命令下载密钥。
zyiz@ubuntu:~$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

如下所示 -

zyiz@ubuntu:~$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: /tmp/tmp.Eix5oqerzZ/gpg.1.sh --keyserver
hkp://ha.pool.sks-keyservers.net:80
--recv-keys
58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server ha.pool.sks-keyservers.net
gpg: key 2C52609D: public key "Docker Release Tool (releasedocker) <docker@docker.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
zyiz@ubuntu:~$
  1. 运行以下命令,它将替换文件的操作系统的相应条目。
zyiz@ubuntu:~$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list

执行上面命令,输出结果如下 -

zyiz@ubuntu:~$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list
<REPO>
zyiz@ubuntu:~$
  1. 打开文件/etc/apt/sources.list.d/docker.list并将以下行粘贴到文件中。
deb http://apt.dockerproject.org/repo ubuntu-xenial main

使用 vi 创建并打开文件 /etc/apt/sources.list.d/docker.list 如下所示 -

zyiz@ubuntu:~$ sudo vi /etc/apt/sources.list.d/docker.list
zyiz@ubuntu:~$
  1. 现在再次更新apt包索引。
zyiz@ubuntu:~$ sudo apt-get update

执行更新apt包索引,添加 docker 源地址,上面语句执行结果如下 -

zyiz@ubuntu:~$ sudo apt-get update
Get:1 http://apt.dockerproject.org/repo ubuntu-xenial InRelease [48.7 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:3 http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages [4,177 B]
Hit:4 http://nginx.org/packages/ubuntu xenial InRelease
Hit:5 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Get:6 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Fetched 359 kB in 20s (17.3 kB/s)
Reading package lists... Done
zyiz@ubuntu:~$
  1. 验证APT是否从正确的仓库中拉出。
zyiz@ubuntu:~$ sudo apt-cache policy docker-engine

上面语句执行结果如下 -

zyiz@ubuntu:~$ sudo apt-cache policy docker-engine
docker-engine:
  Installed: (none)
  Candidate: 17.05.0~ce-0~ubuntu-xenial
  Version table:
     17.05.0~ce-0~ubuntu-xenial 500
http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     17.04.0~ce-0~ubuntu-xenial 500
http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     17.03.1~ce-0~ubuntu-xenial 500
http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
... ... 
... ...
     1.11.0-0~xenial 500
http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
zyiz@ubuntu:~$
  1. 安装推荐的软件包
zyiz@ubuntu:~$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

执行上面安装命令,输出结果如下 -

zyiz@ubuntu:~$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-                                                                                        extra-virtual
Reading package lists... Done
Building dependency tree
Reading state information... Done
linux-image-extra-4.4.0-31-generic is already the newest version (4.4.0-31.50).
linux-image-extra-4.4.0-31-generic set to manually installed.
The following additional packages will be installed:
  linux-generic linux-headers-4.4.0-78 linux-headers-4.4.0-78-generic
  linux-headers-generic linux-image-4.4.0-78-generic
  linux-image-extra-4.4.0-78-generic linux-image-generic
Suggested packages:
  fdutils linux-doc-4.4.0 | linux-source-4.4.0 linux-tools
Recommended packages:
... ...
... ...
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-4.4.0-78-generic
Found initrd image: /boot/initrd.img-4.4.0-78-generic
Found linux image: /boot/vmlinuz-4.4.0-31-generic
Found initrd image: /boot/initrd.img-4.4.0-31-generic
done
Setting up linux-image-generic (4.4.0.78.84) ...
Setting up linux-headers-4.4.0-78 (4.4.0-78.99) ...
Setting up linux-headers-4.4.0-78-generic (4.4.0-78.99) ...
Setting up linux-headers-generic (4.4.0.78.84) ...
Setting up linux-generic (4.4.0.78.84) ...
Setting up linux-image-extra-virtual (4.4.0.78.84) ...
zyiz@ubuntu:~$

安装最新的Docker版本

  1. 更新apt包索引。
zyiz@ubuntu:~$ sudo apt-get update

执行上面代码,结果如下 -

zyiz@ubuntu:~$ sudo apt-get update
[sudo] password for zyiz:
Hit:1 http://apt.dockerproject.org/repo ubuntu-xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Hit:3 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Hit:5 http://nginx.org/packages/ubuntu xenial InRelease
Get:6 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Fetched 306 kB in 9s (31.2 kB/s)
Reading package lists... Done
zyiz@ubuntu:~$
  1. 安装Docker引擎。
zyiz@ubuntu:~$ sudo apt-get install docker-engine

执行上面代码,结果如下 -

zyiz@ubuntu:~$ sudo apt-get install docker-engine
... ...
... ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode
Setting up aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Setting up cgroupfs-mount (1.2) ...
Setting up libltdl7:amd64 (2.4.6-0.1) ...
Setting up docker-engine (17.05.0~ce-0~ubuntu-xenial) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.1) ...
Setting up git (1:2.7.4-0ubuntu1.1) ...
Setting up patch (2.7.5-1) ...
Setting up rename (0.20-4) ...
update-alternatives: using /usr/bin/file-rename to provide /usr/bin/rename (rename) in auto mode
Setting up xz-utils (5.1.1alpha+20120614-2ubuntu2) ...
update-alternatives: using /usr/bin/xz to provide /usr/bin/lzma (lzma) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
zyiz@ubuntu:~$
  1. 启动docker守护进程。
zyiz@ubuntu:~$ sudo service docker start
  1. 通过运行hello-world映像来验证Docker是否正确安装。
zyiz@ubuntu:~$ sudo docker run hello-world

如下面的屏幕截图 -

以上命令下载测试映像并在容器中运行。当容器运行时,它会打印一条消息并退出。


上一篇:Docker架构

下一篇:Docker容器和映像

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程