《Docker技术入门与实战》

一、什么是docker

Docker最初是dotCloud公司的一个内部项目,于2013年3月以Apache 2.0授权协议开源。Docker自开源后就受到广泛的关注和讨论,甚至由于Docker项目的火爆,dotCloud公司在2013年底更名为Docker。

Docker使用Go语言开发实现,基于Linux内核的cgroupnamespace,以及AUFS类的Union FS等技术,对进程进行封装隔离,属于操作系统级虚拟化技术。容器技术最初由LXC实现,隔离进程独立于宿主机,Docker在容器的基础上,进行了进一步的封装,从文件系统、网络互连到进程隔离,极大的简化了容器的创建和维护。

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Docker VS virtual machine

 

基本概念

镜像

Docker镜像是一个特殊的文件系统,它包含了应用程序运行时所需要的代码、运行时、库、环境变量和配置文件。镜像不包含任何动态数据,其内容在构建之后就不会再改变。镜像利用Union FS,分层存储,通常,一个镜像都是基于另一个镜像构建而成。

容器

镜像(image)和容器(container)的关系就像面向对象程序设计中对象的关系,镜像是静态的定义,容器是镜像运行时的实体。

容器的实质是进程,但它与直接运行在系统上的进程又有区别,每个容器进程运行在一个独立的命名空间(namespace)。因此,它有独立的文件系统,网络、进程空间、用户ID空间。 容器也是分层存储的,每一个容器运行时,以镜像为基础层,在其上创建一个当前容器的存储层,容器运行时的所有文件写入操作都写在这个存储层中。

容器的存储层生命周期和容器一样,容器消亡时,容器存储层也就消亡了。因此,任何保存在容器存储层中的数据也将消失。

Registry

Docker Registry是一个集中存储、分发镜像的服务。

一个Docker Registry中可以包含多个仓库(Repository);每个仓库可以包含多个标签(tag),每个标签对应一个镜像。

底层原理

基本架构

Docker是一个C/S架构的程序,提供了一个命令行工具docker以及一整套RESTful API。

namespace

Docker使用命名空间技术来提供容器间的隔离。每当你运行一个容器,Docker就给这个容器创建一系列namespace

  • The pid namespace: Process isolation (PID: Process ID).
  • The net namespace: Managing network interfaces (NET: Networking).
  • The ipc namespace: Managing access to IPC resources (IPC: InterProcess Communication).
  • The mnt namespace: Managing filesystem mount points (MNT: Mount).
  • The uts namespace: Isolating kernel and version identifiers. (UTS: Unix Timesharing System).

cgroup

cgroup的全称是Linux control groups,它提供了应用程序访问资源的限制。

如:限制容器使用的cpumemory

UnionFS

https://docs.docker.com/v17.03/engine/userguide/storagedriver/imagesandcontainers/

Ideally, very little data is written to a container’s writable layer, and you use Docker volumes to write data. However, some workloads require you to be able to write to the container’s writable layer. This is where storage drivers come in.

Docker uses storage drivers to manage the contents of the image layers and the writable container layer. Each storage driver handles the implementation differently, but all drivers use stackable image layers and the copy-on-write (CoW) strategy.

Docker supports several different storage drivers, using a pluggable architecture. The storage driver controls how images and containers are stored and managed on your Docker host.

AUFS

AUFS是一种Union File System,所谓UnionFS就是把不同物理位置的目录合并mount到同一个目录中。

https://docs.docker.com/v17.03/engine/userguide/storagedriver/aufs-driver/

devicemapper

https://docs.docker.com/v17.03/engine/userguide/storagedriver/device-mapper-driver/

overlayFS

https://docs.docker.com/v17.03/engine/userguide/storagedriver/overlayfs-driver/

BTRFS

https://docs.docker.com/v17.03/engine/userguide/storagedriver/btrfs-driver/

networking

  • bridge
  • host
  • overlay
  • macvlan 
    Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses.
  • none

https://docs.docker.com/network/

为什么要用docker

  1. 更高效的利用系统资源
  2. 更快速的启动时间
  3. 一致的运行环境
  4. 更轻松的迁移和扩展

如何使用docker

安装docker

  • Community Edition (CE)
  • Enterprise Edition (EE)
$ yum install docker-ce-<VERSION STRING>

https://docs.docker.com/install/linux/docker-ce/centos/

使用镜像

docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]

eg:
docker pull alpine

使用dockerfile制作镜像

FROM ubuntu:14.04
CMD ["/bin/echo" , "Hi Docker !"]

$ docker build -t helloword:v1 .

使用docker save 和docker load

$ docker save alpine | gzip > alpine-latest.tar.gz

$ docker load -i alpine-latest.tar.gz

操作容器

启动:

$ docker run ubuntu:14.04 /bin/echo 'Hello world'

$ docker run -t -i ubuntu:14.04 /bin/bash

其中,-t选项让Docker分配一个伪终端(pseudo-tty) 并绑定到容器的标准输入上, -i则让容器的标准输入保持打开。

终止:

docker container stop CONTAINER

删除:

docker container rm CONTAINER

数据管理

  • volumes
  • bind mounts
  • tmpfs mounts

创建volume:

docker volume create my-vol

挂载:

$ docker run -d \
  --name devtest \
  --mount source=my-vol,target=/app \
  nginx:latest

删除:

$ docker container stop devtest
$ docker container rm devtest
$ docker volume rm my-vol

bind mounts:

$ docker run -d \
  -it \
  --name devtest \
  -p 80:80 \
  --mount type=bind,source=index.html,target=/usr/share/nginx/html/index.html \
  nginx:latest

tmpfs mounts

$ docker run -d \
  -it \
  --name tmptest \
  --mount type=tmpfs,destination=/app \
  nginx:latest

https://docs.docker.com/storage/

容器编排

  • docker swarm
  • kubernetes

参考: 
《Docker——从入门到实践》 
《第一本Docker书》 
《Kubernetes 指南》
https://docs.docker.com 
https://github.com/feiskyer/kubernetes-handbook

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值