目录

学习 Docker(1)-入门

简介

环境配置的问题

软件开发中的一大问题,就是环境配置。开发、测试和生产环境不完全相同,需要复杂的配置才能在新机器上部署代码。

虚拟机和 Linux 容器

虚拟机(virtual machine)可以在操作系统里虚拟出另一个操作系统,可以将配置好环境的操作系统做成镜像复制到其他机器上运行,是带环境安装的一种解决方案。但由于虚拟机是虚拟化了整个操作系统,因此有资源占用多、冗余步骤多和启动慢的缺点。

Linux 容器(Linux Container,缩写为 LXC)是另一种虚拟化方案。它不是模拟一个完整的操作系统,而是对进程进行隔离。Linux 容器是进程级别的,容器中的进程使用的资源都是虚拟的。相比虚拟机,Linux 容器启动快、资源占用少、体积小。

Docker 是什么

Docker 是 PaaS 提供商 dotCloud 开源的一个基于 LXC 的高级容器引擎,源代码托管在 Github 上,基于 go 语言并遵从 Apache2.0 协议开源。是目前最流行的 Linux 容器解决方案。

architecture.svg

Docker 将应用程序和该程序的依赖,打包在镜像(Image)文件中。运行文件可以生成容器(Container),程序就在容器中运行。Docker 接口简单,用户可以方便的构建和使用容器,并进行版本管理、复制、分享、修改等操作。

Docker 分为 CE(社区版)和 EE(企业版)两个版本,企业版包含了一些收费服务,个人开发者一般用不到。

Docker 的用途

Docker 主要有三大用途。

  • 提供一次性的运行环境。如本体测试他人的软件、持续集成的时候提供单元测试和构建的环境。
  • 提供弹性的云服务。Docker 容器随开随关的特性,很适合动态扩容和缩容。
  • 组建微服务架构。在一台机器上跑多个容器,每个容器运行不同的服务,在本机就可以模拟出微服务架构。

Docker 的基本组成

Docker 包含了以下几个主要部分:Client(客户端)、Deamon(守护进程)、Image(镜像)、Container(容器)和 Registry(仓库)。

  • Docker 为 C/S 架构,用户通过 Client 向 Deamon 发出指令,Deamon 处理完后返回结果给 Client;
  • Image 是 Container 的模版文件,定义了代码和执行环境;
  • Container 是代码执行单元,由 Image 新建一个 Container 来执行代码;
  • Registry 类似 Github,可以为公开或私有,用来存储和管理镜像,官方公开仓库 Docker Hub

类比 Docker 和 Supervisor:

DockerSupervisor
Clientsupervisorctl
Deamonsupervisord
Imageprogram 的配置文件,program01.conf
Containersupervisord 根据 program 配置文件启动的应用进程。

安装

MacOS

1
brew cask install docker

Ubuntu 和 树莓派

Ubuntu 参考 Get Docker CE for Ubuntu | Docker Documentation

树莓派参考 Get Docker CE for Debian | Docker Documentation

一些安全方面的配置 Post-installation steps for Linux | Docker Documentation

安装完成后

验证安装是否成功:

1
2
3
docker version
# or
docker info

docker 服务管理:

1
2
3
4
5
# service 命令的用法
$ sudo service docker start

# systemctl 命令的用法
$ sudo systemctl start docker

上手实例

hello world

从 官方的 Docker Hub 抓取 image 到本地:

1
docker pull library/hello-world

docker image pull 从仓库抓取 image 文件,library 是 image 文件在仓库中所在的组,hello-world 是 image 文件的名字。

官方提供的 image 都放在 library 组中,该组是默认组,因此组名可以省略:

1
docker pull hello-world

抓取后,查看本机的 image 文件:

1
docker images

创建 container 运行

1
docker run hello-world

docker run 命令会从 image 文件,生成一个正在运行的 container 实例。如果本地没有指定的 image 文件,会自动从仓库抓取,前面的 docker pull 命令并不是必要的。

运行成功后,会有这样一段输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ 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.
    (amd64)
 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://hub.docker.com/

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

输出这段提示以后,hello-world 就会停止运行,container 自动终止。

Ubuntu

如果 container 提供的是服务,运行结束后不会自动终止。比如 ubuntu

1
docker run -it ubuntu

就可以在命令行中体验 Ubuntu 系统。这里我们直接运行了 library/ubuntu,Docker 会自动帮我们拉取镜像。

参考链接