Docker安裝

可以在任何操作系統上安裝Docker,無論是Mac,Windows,Linux還是任何雲服務器。Docker 引擎在Linux發行版上運行。 在這裏,我們將以Linux Ubuntu Xenial-16.04 [LTS]作爲演示安裝Docker引擎的過程。

前提條件:

Docker需要兩個重要的安裝要求:

  • 它僅適用於64位Linux安裝,注意:是64位的Linux系統。
  • 它需要Linux內核版本3.10或更高版本。

要查看當前的內核版本,請打開終端並鍵入uname -r命令以查看內核版本:

查看內核版本命令如下:

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

查看操作系統是32位還是64位:

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

更新apt源

按照以下說明更新apt源。

  1. 打開一個終端窗口。
  2. 使用sudo命令作爲root用戶登錄。
  3. 更新包信息並安裝CA證書。

命令:

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

參見下面的屏幕截圖 -

Docker安裝

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

如下所示 -

yiibai@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) <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
yiibai@ubuntu:~$
  1. 運行以下命令,它將替換文件的操作系統的相應條目。
yiibai@ubuntu:~$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list

執行上面命令,輸出結果如下 -

yiibai@ubuntu:~$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list
<REPO>
yiibai@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 如下所示 -

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

執行更新apt包索引,添加 docker 源地址,上面語句執行結果如下 -

yiibai@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
yiibai@ubuntu:~$
  1. 驗證APT是否從正確的倉庫中拉出。
yiibai@ubuntu:~$ sudo apt-cache policy docker-engine

上面語句執行結果如下 -

yiibai@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
        500 http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     17.04.0~ce-0~ubuntu-xenial 500
        500 http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     17.03.1~ce-0~ubuntu-xenial 500
        500 http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
... ... 
... ...
     1.11.0-0~xenial 500
        500 http://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
yiibai@ubuntu:~$
  1. 安裝推薦的軟件包
yiibai@ubuntu:~$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

執行上面安裝命令,輸出結果如下 -

yiibai@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) ...
yiibai@ubuntu:~$

安裝最新的Docker版本

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

執行上面代碼,結果如下 -

yiibai@ubuntu:~$ sudo apt-get update
[sudo] password for yiibai:
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
yiibai@ubuntu:~$
  1. 安裝Docker引擎。
yiibai@ubuntu:~$ sudo apt-get install docker-engine

執行上面代碼,結果如下 -

yiibai@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) ...
yiibai@ubuntu:~$
  1. 啓動docker守護進程。
yiibai@ubuntu:~$ sudo service docker start
  1. 通過運行hello-world映像來驗證Docker是否正確安裝。
yiibai@ubuntu:~$ sudo docker run hello-world

如下面的屏幕截圖 -

Docker安裝

以上命令下載測試映像並在容器中運行。當容器運行時,它會打印一條消息並退出。