[Docker-01] 我尝试安装了Docker

我正在写关于我在Docker相关方面尝试过的事情。由于是第一次尝试,所以我将从安装开始进行。这篇文章主要记录了我的操作过程。

由于许多人已经解释了“Docker是什么?”或者“它的工作原理是什么?”之类的问题,我会省略掉这部分解释。

准备Docker主机

我会使用Vagrant安装Ubuntu。
这次我们将尝试使用Ubuntu Vivid 15.04,所以我已经创建了以下的Vagrantfile。

Vagrant.configure(2) do |config|
  config.vm.define :docker1 do |node|
    node.vm.box = 'ubuntu/vivid64'
    node.vm.network 'private_network', ip: '192.168.100.10'
  end
end

使用”vagrant up”命令来启动。

$ vagrant up

使用vagrant ssh命令登录(docker1是在Vagrantfile中指定的节点名称)。

$ vagrant ssh docker1

安装Docker

请按照Docker安装指南进行安装。
https://docs.docker.com/engine/installation/ubuntulinux/

检查内核是否为3.10以上

据说如果内核版本低于3.10,就无法正常运行,所以我将查找内核版本。

vagrant@vagrant-ubuntu-vivid-64:~$ uname -r
3.19.0-43-generic

没问题。

在apt中注册Docker的存储库。

Docker的软件包似乎是通过Docker提供的存储库进行下载的,所以需要将其添加到apt的源中。

首先,我们需要注册gpg密钥(以下是实际执行的命令,但由于关键性质,请确保查阅官方安装指南以确保操作正确)。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.eqX8aVFex6 --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server p80.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)

接下来将添加source的定义。
安装指南中要求创建以下文件。

/docker.list 是位于 /etc/apt/sources.list.d/ 目录下的文件。

根据不同版本,上述文件中的内容会有所不同。由于本次是Ubuntu Vivid 15.04版本,因此我做了以下的设定。

deb https://apt.dockerproject.org/repo ubuntu-vivid main

只需要将「ubuntu-vivid」替换为14.04则是trusty,15.10则是wily即可。

因为添加了源,所以需要更新apt的索引。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude update
Get: 1 https://apt.dockerproject.org ubuntu-vivid InRelease [442 B]
Ign https://apt.dockerproject.org ubuntu-vivid InRelease
Get: 2 https://apt.dockerproject.org ubuntu-vivid Release.gpg [442 B]
Hit http://archive.ubuntu.com vivid InRelease
Hit http://security.ubuntu.com vivid-security InRelease
Get: 3 https://apt.dockerproject.org ubuntu-vivid Release
Get: 4 https://apt.dockerproject.org ubuntu-vivid/main amd64 Packages
Hit http://archive.ubuntu.com vivid-updates InRelease
Get: 5 http://security.ubuntu.com vivid-security/main Sources [57.5 kB]
Get: 6 https://apt.dockerproject.org ubuntu-vivid/main Translation-en
Get: 7 http://archive.ubuntu.com vivid-backports InRelease [64.5 kB]
(後略)

由于安装指南中提到了要删除lxc-docker,所以我们先确认一下它是否没有安装。

vagrant@vagrant-ubuntu-vivid-64:~$ aptitude search lxc
p   libclxclient-dev                             - Development file for libclxclient
p   libclxclient3                                - X Window System C++ access library
v   liblxc0                                      -
p   liblxc1                                      - Linux Containers userspace tools (library)
p   lua-lxc                                      - Linux Containers userspace tools (LUA bindings)
p   lxc                                          - Linux Containers userspace tools
p   lxc-android-config                           - configuration to fire up an ubuntu-touch android conta
p   lxc-dbg                                      - Linux Containers userspace tools (debug)
p   lxc-dev                                      - Linux Containers userspace tools (development)
p   lxc-templates                                - Linux Containers userspace tools (templates)
p   lxc-tests                                    - Linux Containers userspace tools (test binaries)
p   lxcfs                                        - FUSE based filesystem for LXC
p   lxctl                                        - Utility to manage LXC
p   nova-compute-lxc                             - OpenStack Compute - compute node (LXC)
p   python-lxc                                   - Linux container userspace tools (Python 2.x bindings)
p   python3-lxc                                  - Linux Containers userspace tools (Python 3.x bindings)
p   unity8-lxc                                   - Unity8 LXC integration

有人提醒要确认一下docker-engine是否已从Docker的存储库中下载,我会进行确认。

vagrant@vagrant-ubuntu-vivid-64:~$ apt-cache policy docker-engine
docker-engine:
  Installed: (none)
  Candidate: 1.9.1-0~vivid
  Version table:
     1.9.1-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.9.0-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.8.3-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.8.2-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.8.1-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.8.0-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.7.1-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.7.0-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.6.2-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.6.1-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.6.0-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages
     1.5.0-0~vivid 0
        500 https://apt.dockerproject.org/repo/ ubuntu-vivid/main amd64 Packages

看起来很健壮。
我们先在这里进行一次升级,更新软件包。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude upgrade

安装linux-image-extra

为了使用aufs存储驱动程序,在Linux系统上安装名为linux-image-extra的软件包。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude install linux-image-extra-$(uname -r)
The following NEW packages will be installed:
  crda{a} iw{a} libnl-3-200{a} libnl-genl-3-200{a} linux-image-extra-3.19.0-43-generic
  wireless-regdb{a}
0 packages upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 38.6 MB of archives. After unpacking 162 MB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://archive.ubuntu.com/ubuntu/ vivid/main libnl-3-200 amd64 3.2.24-2 [47.3 kB]
Get: 2 http://archive.ubuntu.com/ubuntu/ vivid/main libnl-genl-3-200 amd64 3.2.24-2 [10.6 kB]
Get: 3 http://archive.ubuntu.com/ubuntu/ vivid-updates/main wireless-regdb all 2014.11.18-1ubuntu1~ubuntu15.04.1 [8290 B]
(後略)

另外,对于Ubuntu Precise 12.04(LTS)的情况,安装方法似乎有所不同,请查阅官方的安装指南。

安装

终于我要安装docker-engine了。
说起来也只是普通地通过apt安装而已。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo aptitude install docker-engine
The following NEW packages will be installed:
  aufs-tools{a} cgroupfs-mount{a} docker-engine
0 packages upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 7472 kB of archives. After unpacking 37.7 MB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://archive.ubuntu.com/ubuntu/ vivid/universe aufs-tools amd64 1:3.2+20130722-1.1 [92.3 kB]
Get: 2 https://apt.dockerproject.org/repo/ ubuntu-vivid/main docker-engine amd64 1.9.1-0~vivid [7375 kB]
Get: 3 http://archive.ubuntu.com/ubuntu/ vivid/universe cgroupfs-mount all 1.1 [4796 B]
Fetched 7472 kB in 2s (3373 kB/s)
Selecting previously unselected package aufs-tools.
(Reading database ... 70426 files and directories currently installed.)
Preparing to unpack .../aufs-tools_1%3a3.2+20130722-1.1_amd64.deb ...
Unpacking aufs-tools (1:3.2+20130722-1.1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../cgroupfs-mount_1.1_all.deb ...
Unpacking cgroupfs-mount (1.1) ...
Selecting previously unselected package docker-engine.
Preparing to unpack .../docker-engine_1.9.1-0~vivid_amd64.deb ...
Unpacking docker-engine (1.9.1-0~vivid) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (219-7ubuntu6) ...
Setting up aufs-tools (1:3.2+20130722-1.1) ...
Setting up cgroupfs-mount (1.1) ...
Setting up docker-engine (1.9.1-0~vivid) ...
Processing triggers for libc-bin (2.21-0ubuntu4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (219-7ubuntu6) ...

启动Docker服务。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo service docker start

让我们尝试运行hello-world。

在公式的安装指南中也提到了,我们要启动一个名为hello-world的容器,来确认安装是否成功。

在使用docker命令之前,每次都需要使用sudo很麻烦,所以要使普通用户(vagrant)能够使用docker命令,需要将普通用户加入docker组。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo gpasswd -a vagrant docker
Adding user vagrant to group docker

在完成上述操作后,您需要先注销,然后再次进行登录。接下来,我们尝试启动hello-world。

vagrant@vagrant-ubuntu-vivid-64:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

b901d36b6f2f: Pull complete
0a6ba66e537a: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
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 Hub account:
 https://hub.docker.com

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

似乎有动过。

在消息中提到“尝试运行$ docker run -it ubuntu bash”,我会试一下。

vagrant@vagrant-ubuntu-vivid-64:~$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
fcee8bcfe180: Pull complete
4cdc0cbc1936: Pull complete
d9e545b90db8: Pull complete
c4bea91afef3: Pull complete
Digest: sha256:b53bb7b0d18842214ac7472c2a8801e8682c247d30f1ba4bab0083a2e2e091ea
Status: Downloaded newer image for ubuntu:latest
root@61816c709164:/# uname -a
Linux 61816c709164 3.19.0-43-generic #49-Ubuntu SMP Sun Dec 27 19:43:07 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@61816c709164:/# /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

就是这样了。

我下载了Ubuntu的镜像并启动了一个容器,看起来在容器内启动了bash。

root@61816c709164:/# 这是容器的提示符。在上面的例子中,我试着在容器内运行了uname -a和/sbin/ifconfig命令。

设置在启动时自动启动Docker。

vagrant@vagrant-ubuntu-vivid-64:~$ sudo systemctl enable docker
Synchronizing state for docker.service with sysvinit using update-rc.d...
Executing /usr/sbin/update-rc.d docker defaults
Executing /usr/sbin/update-rc.d docker enable