标签归档:虚拟化

Ubuntu 16.04 上安装Docker

Docker是一个轻量级的虚拟化容器技术,既可以为不同应用程序提供了隔离空间(内存,资源,网络),又可以将应用程序与环境一起打包,发布到其他机器上,避免环境的差异,这点像Vagrant。 但是,它又与传统的虚拟机不容(如VirtualBox),它的应用程序仍然是跑在宿主机上面的,并且镜像里面通常仅包含所必须的组件而非完整的系统,因此拥有极高的性能。这又有点像Python的Virtual Environment,不过Docker更加强大,本质是是个Linux,可以打包的不同语言的应用程序。
Docker要求linux内核版本大于3.10,可以在通过以下命令查看是否支持

$ uname -r
4.4.0-38-generic

需要先更新以下apt仓库

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
[/sell]
检查以下是否已经创建Docker软件仓库文件,如果没有需要手动创建,并添加以下内容

root@thinkpad:~# cat /etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo ubuntu-xenial main
[/sell]
确认是否配置成功

root@thinkpad:~# sudo apt-get update
root@thinkpad:~# apt-cache policy docker-engine
docker-engine:
  Installed: 1.12.1-0~xenial
  Candidate: 1.12.1-0~xenial
  Version table:
 *** 1.12.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
        100 /var/lib/dpkg/status
     1.12.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.2-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages

Docker容器建立在Auf文件系统之上,以便支持将不同的目录挂载到同一虚拟文件系统下面,并设置不同的读写权限。需要安装linux-image-extra:

$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
[/sell]
现在可以开始安装Docker了:

$ sudo apt-get install docker-engine
$ sudo service docker start

试运行一下:

root@thinkpad:~# docker run hello-world

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/engine/userguide/

如果你本地没有hello-world这个镜像,它是会自动去Docker官网上下载的。
查看最近运行过的容器,不加参数-l,是查看正在运行的容器

root@thinkpad:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1931765938dc        hello-world         "/hello"            4 minutes ago       Exited (0) 4 minutes ago                       suspicious_lichterman

运行镜像的时候也可以指定名称(而不是suspicious_lichterman)

root@thinkpad:~# docker run --name hello hello-world

Hello from Docker!
...

root@thinkpad:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
e4773606e6cf        hello-world         "/hello"            7 seconds ago       Exited (0) 6 seconds ago                       hello

但是再次运行同样名称为hello的容器时会报错

root@thinkpad:~# docker run --name hello hello-world
docker: Error response from daemon: Conflict. The name "/hello" is already in use by container e4773606e6cf4222b786d5301ab38e4dddb90e11a0358f64d33109bf829f0b5a. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.

这时候需要先把旧的给删掉,再运行就不会了

root@thinkpad:~# docker rm hello
hello

通过Docker stop停止运行指定容器。
查看本地的Docker镜像

root@thinkpad:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-php-fpm       phalcon             1c97ee169a55        3 hours ago         364.5 MB
nginx-php-fpm       latest              4fc9ac9f2945        7 hours ago         228.5 MB
ubuntu              16.04               c73a085dc378        47 hours ago        127.1 MB
ubuntu              latest              c73a085dc378        47 hours ago        127.1 MB
nginx               mainline-alpine     00bc1e841a8f        5 days ago          54.21 MB
mysql               5.7                 4b3b6b994512        5 weeks ago         384.5 MB
hello-world         latest              c54a2cc56cbb        12 weeks ago        1.848 kB

这里先简单的介绍Docker的安装、运行,后续将介绍Docker命令行交互,网络,文件存储等。

参考链接:
Docker Overview
Docker:利用Linux容器实现可移植的应用部署
Installation
on Ubuntu

Docker:具备一致性的自动化软件部署
Docker — 从入门到实践
Docker简介与入门
剖析Docker文件系统:Aufs与Devicemapper
What is the runtime performance cost of a Docker container
Docker – The Linux Container
当流浪者(Vagrant)遇见码头工人(Docker): 实战