阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Docker学习笔记

156次阅读
没有评论

共计 20058 个字符,预计需要花费 51 分钟才能阅读完成。

什么是 Docker

Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护。

Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案。Docker 的基础是 Linux 容器(LXC)等技术。

在 LXC 的基础上 Docker 进行了进一步的封装,让用户不需要去关心 容器 的管理,使得操作更为简便。用户操作 Docker 的 容器 就像操作一个快速轻量级的 虚拟机 一样简单。

下面的图片比较了 Docker 和传统虚拟化方式的不同之处,可见 容器 是在操作系统层面上实现虚拟化,直接复用本地主机的操作系统,而传统方式则是在硬件层面实现。

Docker 学习笔记

 

Docker 学习笔记

为什么要使用 Docker?

首先,Docker 容器 的启动可以在秒级实现,这相比传统的 虚拟机 方式要快得多。其次,Docker 对系统资源的利用率很高,一台主机上可以同时运行数千个 Docker 容器

容器 除了运行其中应用外,基本不消耗额外的系统资源,使得应用的性能很高,同时系统的开销尽量小。传统 虚拟机 方式运行 10 个不同的应用就要起 10 个 虚拟机,而 Docker 只需要启动 10 个隔离的应用即可。

更多 Docker 相关教程见以下内容

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Ubuntu 14.04 安装 Docker  http://www.linuxidc.com/linux/2014-08/105656.htm 

Ubuntu 使用 VNC 运行基于 Docker 的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm 

Ubuntu 15.04 下安装 Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm 

在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm 

在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

具体说来,Docker 在如下几个方面具有较大的优势。

1) 更快速的交付和部署

对开发和运维(devop)人员来说,最希望的就是一次创建或配置,可以在任意地方正常运行。

开发者可以使用一个标准的镜像来构建一套开发 容器 ,开发完成之后,运维人员可以直接使用这个 容器 来部署代码。Docker 可以快速创建 容器 ,快速迭代应用程序,并让整个过程全程可见,使团队中的其他成员更容易理解应用程序是如何创建和工作的。Docker 容器 很轻很快!容器 的启动时间是秒级的,大量地节约开发、测试、部署的时间。

2) 更高效的虚拟化

Docker 容器 的运行不需要额外的 hypervisor 支持,它是内核级的虚拟化,因此可以实现更高的性能和效率。

3) 更轻松的迁移和扩展

Docker 容器 几乎可以在任意的平台上运行,包括物理机、虚拟机、公有云、私有云、个人电脑、服务器等。这种兼容性可以让用户把一个应用程序从一个平台直接迁移到另外一个。

4) 更简单的管理

使用 Docker,只需要小小的修改,就可以替代以往大量的更新工作。所有的修改都以增量的方式被分发和更新,从而实现自动化并且高效的管理。

5) 对比传统 虚拟机 总结

特性
容器
虚拟机
启动 秒级 分钟级
硬盘使用 一般为 MB 一般为 GB
性能 接近原生 弱于
系统支持量 单机支持上千个 容器 一般几十个

Docker 安装(Ubuntu 14.04)

1) 通过系统自带包安装

Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装。

sudo apt-get update
sudo apt-get install -y docker.io
sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

如果使用操作系统自带包安装 Docker,目前安装的版本是比较旧的 1.0.0。要安装更新的版本,可以通过使用 Docker 源的方式。之后要讲到的 Docker 加速器需要 1.3.2 版本以上,所以建议直接使用 Docker 源的方式安装最新版本。

2) 通过 Docker 源安装最新版本

要安装最新的 Docker 版本,首先需要安装 apt-transport-https 支持,之后通过添加源来安装。

sudo apt-get install apt-transport-https
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
sudo bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install lxc-docker

Docker 的 安装资源文件 存放在 Amazon S3,会间歇性连接失败。所以安装 Docker 的时候,会比较慢。你可以通过执行下面的命令,高速安装 Docker。

curl -sSL https://get.daocloud.io/docker | sh

适用于 Ubuntu,Debian 等大部分 Linux,会 3 小时同步一次 Docker 官方资源。如果安装不成功,可以选择使用二进制包安装方式。下载二进制包

Docker 的三个基本概念

1) 镜像(Image)

Docker 镜像就是一个只读的模板。

例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了 Apache 或用户需要的其它应用程序。

镜像可以用来创建 Docker 容器。Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。

2) 容器(Container)

Docker 利用 容器 来运行应用。

容器 是从镜像创建的运行实例。它可以被启动、开始、停止、删除。每个 容器 都是相互隔离的、保证安全的平台。可以把 容器 看做是一个简易版的 Linux 环境(包括 root 用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。

* 注:镜像是只读的,容器 在启动的时候创建一层可写层作为最上层。

3) 仓库(Repository)

仓库是集中存放镜像文件的场所。有时候会把仓库和仓库注册服务器 (Registry) 混为一谈,并不严格区分。实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)。

仓库分为公开仓库(Public)和私有仓库(Private)两种形式。最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。当然,用户也可以在本地网络内创建一个私有仓库。当用户创建了自己的镜像之后就可以使用 push 命令将它上传到公有或者私有仓库,这样下次在另外一台机器上使用这个镜像时候,只需要从仓库上 pull 下来就可以了。

* 注:Docker 仓库的概念跟 Git 类似,注册服务器可以理解为 GitHub 这样的托管服务。

Docker 加速器

由于国内网络问题的限制,你要从 Docker Hub 上下载一个镜像,非常慢,可能要等半天。国内就有公司 (DaoCloud) 推出了 Docker 加速器(即 Docker Hub Mirror 服务),要使用该加速器,需要去 DaoCloud 注册一个帐号,得到相应的加速器地址。配置 Docker 加速器命令如下:

echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-mirror=http://×××××.m.daocloud.io\"" | sudo tee -a /etc/default/docker
sudo service docker restart

该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/default/docker 中。适用于 Ubuntu14.04,其他版本可能有细微不同。其中×××××需要替换为你自己的地址。

尝试去拉镜像会发现速度变快很多:

docker pull ubuntu:14.04

Mirror 是 Docker 的官方机制,它是 Registry 的一种特殊类型,在部署了 Registry 之后,需要开启 Mirror 模式并做一定的配置。具体的流程如下:

Docker 学习笔记

Docker Hub 由 Index 和 Registry 构成,Index 保存 Image Layer 的 hash 和关联关系等元数据(Metadata),Registry 用于存储 Image Layer 的实际二进制数据。在客户端没有配置 registry-mirror 参数的情况下,每一次 docker pull,客户端都会先连接 Index 获取元数据,然后再连接 Registry 获取实际的 Image 文件。由于 Docker Hub 的 Index 节点和 Regsitry 都部署国外,国内用户访问,经常遭遇连接超时或中断的情况,下载速度也极其缓慢。在启用了 Mirror 之后,访问流程如下:

  • 客户端的 Docker Daemon 连接 Index 获取 Metadata,这一部分的数据量极小,直连国外的速度可以忍受。
  • 根据 Metadata 的信息,Docker Daemon 与 Mirror 服务器建立连接。如果 pull 的 Image 在 Mirror 上已经有缓存,就直接在 Mirror 上返回地址并下载。
  • 如果 Image 在 Mirror 并无缓存,Mirror 会与 Docker Hub Registry 建立连接,下载 Image,提供给用户的同时,在本地缓存。
  • Mirror 下载 Docker Hub Image 采用 stream 的方式,即可以一边下载,一边提供给客户端的 Docker Daemon,不必等 Image 完全下载完。

通过以上的描述,可以发现,对于常用的 Image,Mirror 缓存命中率会非常高,如 Ubuntu 等基础 Image,这会极大提高下载速度。同时,Docker Image 采用分层的结构,即使 Image 被更新,也只是下载最新一层非常少的增量数据。

Mirror 服务亦可以通过网络优化,加速对远端 Docker Hub Registry 的访问速度,如采用高速的商业 VPN 建立从 Mirror 到 Docker Hub Registry 的访问。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-10/124483p2.htm

使用命令查看一下 docker 都有那些命令:

docker -h

你将得到如下结果:

A self-sufficient runtime for linux containers.

Options:

  --api-cors-header=                   Set CORS headers in the remote API
  -b, --bridge=                        Attach containers to a network bridge
  --bip=                               Specify network bridge IP
  -D, --debug=false                    Enable debug mode
  -d, --daemon=false                   Enable daemon mode
  --default-gateway=                   Container default gateway IPv4 address
  --default-gateway-v6=                Container default gateway IPv6 address
  --default-ulimit=[]                  Set default ulimits for containers
  --dns=[]                             DNS server to use
  --dns-search=[]                      DNS search domains to use
  -e, --exec-driver=native             Exec driver to use
  --exec-opt=[]                        Set exec driver options
  --exec-root=/var/run/docker          Root of the Docker execdriver
  --fixed-cidr=                        IPv4 subnet for fixed IPs
  --fixed-cidr-v6=                     IPv6 subnet for fixed IPs
  -G, --group=docker                   Group for the unix socket
  -g, --graph=/var/lib/docker          Root of the Docker runtime
  -H, --host=[]                        Daemon socket(s) to connect to
  -h, --help=false                     Print usage
  --icc=true                           Enable inter-container communication
  --insecure-registry=[]               Enable insecure registry communication
  --ip=0.0.0.0                         Default IP when binding container ports
  --ip-forward=true                    Enable net.ipv4.ip_forward
  --ip-masq=true                       Enable IP masquerading
  --iptables=true                      Enable addition of iptables rules
  --ipv6=false                         Enable IPv6 networking
  -l, --log-level=info                 Set the logging level
  --label=[]                           Set key=value labels to the daemon
  --log-driver=json-file               Default driver for container logs
  --log-opt=map[]                      Set log driver options
  --mtu=0                              Set the containers network MTU
  -p, --pidfile=/var/run/docker.pid    Path to use for daemon PID file
  --registry-mirror=[]                 Preferred Docker registry mirror
  -s, --storage-driver=                Storage driver to use
  --selinux-enabled=false              Enable selinux support
  --storage-opt=[]                     Set storage driver options
  --tls=false                          Use TLS; implied by --tlsverify
  --tlscacert=~/.docker/ca.pem         Trust certs signed only by this CA
  --tlscert=~/.docker/cert.pem         Path to TLS certificate file
  --tlskey=~/.docker/key.pem           Path to TLS key file
  --tlsverify=false                    Use TLS and verify the remote
  --userland-proxy=true                Use userland proxy for loopback traffic
  -v, --version=false                  Print version information and quit

Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from a container's filesystem to the host path
    create    Create a new container
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    exec      Run a command in a running container
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container or image
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or log in to a Docker registry server
    logout    Log out from a Docker registry server
    logs      Fetch the logs of a container
    pause     Pause all processes within a container
    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
    ps        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    rename    Rename an existing container
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image on the Docker Hub
    start     Start a stopped container
    stats     Display a stream of a containers' resource usage statistics
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

下面来详细说明一些命令的使用,在此之前,说一下如果不理解理解 Docker 和 Docker Hub 及两者关系,可以类比 Git 和 GitHub 理解。

1. docker version

显示 Docker 版本信息。

2. docker info

显示 Docker 系统信息,包括镜像和 容器 数。

3. docker search

从 Docker Hub 中搜索符合条件的镜像。

docker search -s 3 --automated --no-trunc django

上面命令的意思是搜索处收藏数不小于 3,并且能够自动化构建的 django 镜像,并且完整显示镜像描述。

参数:

--automated=false    Only show automated builds 只列出 automated build 类型的镜像--no-trunc=false     Don't truncate output 显示完整的镜像描述
  -s, --stars=0        Only displays with at least x stars 只列出不低于 x 个收藏的镜像

4. docker pull

从 Docker Hub 中拉取或者更新指定镜像。

docker pull Ubuntu:latest

上面命令的意思是拉取 ubuntu 最新的镜像。

参数:

-a, --all-tags=false    Download all tagged images in the repository 拉取所有 tagged 镜像

5. docker login

按步骤输入在 Docker Hub 注册的用户名、密码和邮箱即可完成登录。

6. docker logout

运行后从指定服务器登出,默认为官方服务器。

7. docker images
列出本地所有镜像。对镜像名称进行关键词查询。

docker images ubuntu

上面命令的意思是列出本地镜像名为 ubuntu 的所有镜像。不加 ubuntu,就列出所有本地镜像。

参数:

-a, --all=false      Show all images (default hides intermediate images) 列出所有镜像(含中间映像层,默认情况下,过滤掉中间映像层)  --digests=false      Show digests 展示镜像的摘要
  -f, --filter=[]      Filter output based on conditions provided 过滤镜像,如:-f ['dangling=true'] 只列出满足 dangling=true 条件的镜像
  --no-trunc=false     Don't truncate output 显示完整的镜像 ID
  -q, --quiet=false    Only show numeric IDs 仅列出镜像 ID

8. docker ps

列出所有运行中 容器

参数:

  -a, --all=false       Show all containers (default shows just running) 列出所有  容器(含沉睡镜像)--before=             Show only container created before Id or Name 列出在某一  容器  之前创建的  容器 ,接受 容器  名称和 ID 作为参数
  -f, --filter=[]       Filter output based on conditions provided -f [exited=<int>] 列出满足 exited=<int> 条件的  容器 -l, --latest=false    Show the latest created container, include non-running 仅列出最新创建的一个 容器
  -n=-1                 Show n last created containers, include non-running 列出最近创建的 n 个  容器
  --no-trunc=false      Don't truncate output 显示完整的  容器ID
  -q, --quiet=false     Only display numeric IDs 仅列出  容器ID
  -s, --size=false      Display total file sizes 显示  容器  大小
  --since=              Show created since Id or Name, include non-running 列出在某一  容器  之后创建的  容器 ,接受 容器  名称和 ID 作为参数

9. docker rmi
从本地移除一个或多个指定的镜像。

docker rmi nginx:latest ubuntu:14.04

上面命令的意思是移除 nginx 最新版本的镜像和 ubuntu 14.04 版本的镜像。

参数:

-f, --force=false    Force removal of the image 强行移除该镜像,即使其正被使用
  --no-prune=false     Do not delete untagged parents 不移除该镜像的过程镜像,默认移除

10. docker rm

从本地移除一个或多个指定的 容器

docker rm harrysun/lnmp
docker rm -l webapp/redis

上面命令的意思分别是移除 harrysun/lnmp 的本地 容器 和移除 webapp/redis 容器 的网络连接。

参数:

  -f, --force=false      Force the removal of a running container (uses SIGKILL) 强行移除该  容器 ,即使其正在运行-l, --link=false       Remove the specified link 移除 容器  间的网络连接,而非  容器  本身
  -v, --volumes=false    Remove the volumes associated with the container 移除与  容器  关联的空间

11. docker history

查看指定镜像的创建历史。

docker history -H harrysun/lnmp:0.1

上面命令的意思是查看 harrysun/lnmp:0.1 镜像的历史。

-H, --human=true     Print sizes and dates in human readable format 以可读的格式打印镜像大小和日期
  --no-trunc=false     Don't truncate output 显示完整的提交记录
  -q, --quiet=false    Only show numeric IDs 仅列出提交记录 ID

12. docker start|stop|restart

启动、停止和重启一个或多个指定 容器

docker start -i b5e08e1435b3

上面命令的意思是启动一个 ID 为 b5e08e1435b3 的 容器,并进入交互模式。

参数:

  -a, --attach=false         Attach STDOUT/STDERR and forward signals 启动一个  容器  并打印输出结果和错误
  -i, --interactive=false    Attach container's STDIN 启动一个  容器  并进入交互模式
  -t, --time=10      Seconds to wait for stop before killing the container 停止或者重启  容器  的超时时间(秒),超时后系统将杀死进程。

13. docker kill

杀死一个或多个指定 容器 进程。

docker kill -s KILL 94c6b3c3f04a

上面命令的意思是杀死一个 ID 为 94c6b3c3f04a 的 容器 ,并向 容器 发送 KILL 信号。

参数:

  -s, --signal=KILL    Signal to send to the container 自定义发送至  容器  的信号

14. docker events

从服务器拉取个人动态,可选择时间区间。

docker events --since="20150720" --until="20150808"

上面命令的意思是拉取个人从 2015/07/20 到 2015/08/08 的个人动态。

参数:

-f, --filter=[]    Filter output based on conditions provided--since=           Show all events created since timestamp 开始时间
  --until=           Stream events until this timestamp 结束时间

15. docker save

将指定镜像保存成 tar 归档文件,docker load 的逆操作。保存后再加载(saved-loaded)的镜像不会丢失提交历史和层,可以回滚。

docker save -o ubuntu14.04.tar ubuntu:14.04

上面命令的意思是将镜像 ubuntu:14.04 保存为 ubuntu14.04.tar 文件。

参数:

-o, --output=      Write to an file, instead of STDOUT 输出到的文件

16. docker load

从 tar 镜像归档中载入镜像,docker save 的逆操作。保存后再加载(saved-loaded)的镜像不会丢失提交历史和层,可以回滚。

docker load -i ubuntu14.04.tar

上面命令的意思是将 ubuntu14.04.tar 文件载入镜像中。

参数:

-i, --input=       Read from a tar archive file, instead of STDIN 加载的 tar 文件

17. docker export

将指定的 容器 保存成 tar 归档文件,docker import 的逆操作。导出后导入(exported-imported))的 容器 会丢失所有的提交历史,无法回滚。

docker export -o ubuntu14.04.tar 94c6b3c3f04a

上面命令的意思是将 ID 为 94c6b3c3f04a 容器 保存为 ubuntu14.04.tar 文件。

参数:

-o, --output=      Write to a file, instead of STDOUT

18. docker import

从归档文件(支持远程文件,.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz)创建一个镜像,export 的逆操作,可为导入镜像打上标签。导出后导入(exported-imported))的 容器 会丢失所有的提交历史,无法回滚。

cat ./ubuntu14.04.tar | sudo docker import - ubuntu:14.04

上面命令的意思是使用 ./ubuntu14.04.tar 文件创建 ubuntu:14.04 的镜像,默认会从远端拉取文件。

19. docker top

查看一个正在运行 容器 进程,支持 ps 命令参数。

20. docker inspect

检查镜像或者 容器 的参数,默认返回 JSON 格式。(Template)

docker inspect --format '{{.DockerVersion}}' ubuntu:14.04

上面命令的意思是返回 ubuntu:14.04  镜像的 docker 版本

参数:

-f, --format=      Format the output using the given go template 指定返回值的模板文件

21. docker pause

暂停某一 容器 的所有进程。

22. docker unpause

恢复某一 容器 的所有进程。

23. docker tag

标记本地镜像,将其归入某一仓库。

sudo docker tag 5db5f8471261 harrysun/lnmp:0.2

上面命令的意思是将 ID 为 5db5f8471261 的 容器 标记为 harrysun/lnmp:0.2 镜像。

参数:

-f, --force=false    Force 会覆盖已有标记

24. docker push

将镜像推送至远程仓库,默认为 Docker Hub。

docker push harrysun/lnmp:0.2

上面命令的意思是将 harrysun/lnmp:0.2 镜像推送到远端。

25. docker logs

获取 容器 运行时的输出日志。

docker logs -f --tail 10 94c6b3c3f04a

上面命令的意思是将追踪 ID 为 94c6b3c3f04a 的 容器 最新的 10 条日志。

参数:

  -f, --follow=false        Follow log output 跟踪  容器  日志的最近更新
  --since=                  Show logs since timestamp 开始时间
  -t, --timestamps=false    Show timestamps 显示  容器  日志的时间戳
  --tail=all                Number of lines to show from the end of the logs 仅列出最新 n 条  容器  日志

26. docker run

启动一个 容器,在其中运行指定命令。

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

上面命令的意思是以 ubuntu:14.04 镜像启动一个 容器 ,以交互模式运行,并为 容器 重新分配一个伪输入终端。

参数:(这个命令的参数有点多,只说其中一部分)

  -a, --attach=[]             Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
  --add-host=[]               Add a custom host-to-IP mapping (host:ip)
  --blkio-weight=0            Block IO (relative weight), between 10 and 1000
  -c, --cpu-shares=0          CPU shares (relative weight)
  --cap-add=[]                Add Linux capabilities
  --cap-drop=[]               Drop Linux capabilities
  --cgroup-parent=            Optional parent cgroup for the container
  --cidfile=                  Write the container ID to the file
  --cpu-period=0              Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0               Limit the CPU CFS quota
  --cpuset-cpus=              CPUs in which to allow execution (0-3, 0,1) 绑定  容器  到指定 CPU 运行
  --cpuset-mems=              MEMs in which to allow execution (0-3, 0,1) 绑定  容器  到指定 MEM 运行
  -d, --detach=false          Run container in background and print container ID 后台运行  容器 ,并返回 容器ID
  --device=[]                 Add a host device to the container
  --dns=[]                    Set custom DNS servers 指定  容器  使用的 DNS 服务器,默认和宿主一致
  --dns-search=[]             Set custom DNS search domains 指定  容器DNS 搜索域名,默认和宿主一致
  -e, --env=[]                Set environment variables 设置环境变量
  --entrypoint=               Overwrite the default ENTRYPOINT of the image
  --env-file=[]               Read in a file of environment variables 从指定文件读入环境变量
  --expose=[]                 Expose a port or a range of ports
  -h, --hostname=             Container host name 指定  容器  的 hostname
  --help=false                Print usage
  -i, --interactive=false     Keep STDIN open even if not attached 以交互模式运行  容器,通常与 -t 同时使用
  --ipc=                      IPC namespace to use
  -l, --label=[]              Set meta data on a container
  --label-file=[]             Read in a line delimited file of labels
  --link=[]                   Add link to another container
  --log-driver=               Logging driver for container
  --log-opt=[]                Log driver options
  --lxc-conf=[]               Add custom lxc options
  -m, --memory=               Memory limit
  --mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --memory-swap=              Total memory (memory + swap), '-1' to disable swap
  --name=                     Assign a name to the container 为  容器  指定一个名称
  --net=bridge                Set the Network mode for the container  指定  容器  的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型
  --oom-kill-disable=false    Disable OOM Killer
  -P, --publish-all=false     Publish all exposed ports to random ports
  -p, --publish=[]            Publish a container's port(s) to the host
  --pid=                      PID namespace to use
  --privileged=false          Give extended privileges to this container
  --read-only=false           Mount the container's root filesystem as read only
  --restart=no                Restart policy to apply when a container exits
  --rm=false                  Automatically remove the container when it exits
  --security-opt=[]           Security Options
  --sig-proxy=true            Proxy received signals to the process
  -t, --tty=false             Allocate a pseudo-TTY 为  容器  重新分配一个伪输入终端,通常与 -i 同时使用
  -u, --user=                 Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                 Ulimit options
  --uts=                      UTS namespace to use
  -v, --volume=[]             Bind mount a volume
  --volumes-from=[]           Mount volumes from the specified container(s)
  -w, --workdir=              Working directory inside the container 

暂且就先到这里了,有什么问题,欢迎大家随时留言~~

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-10/124483.htm

什么是 Docker

Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护。

Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案。Docker 的基础是 Linux 容器(LXC)等技术。

在 LXC 的基础上 Docker 进行了进一步的封装,让用户不需要去关心 容器 的管理,使得操作更为简便。用户操作 Docker 的 容器 就像操作一个快速轻量级的 虚拟机 一样简单。

下面的图片比较了 Docker 和传统虚拟化方式的不同之处,可见 容器 是在操作系统层面上实现虚拟化,直接复用本地主机的操作系统,而传统方式则是在硬件层面实现。

Docker 学习笔记

 

Docker 学习笔记

为什么要使用 Docker?

首先,Docker 容器 的启动可以在秒级实现,这相比传统的 虚拟机 方式要快得多。其次,Docker 对系统资源的利用率很高,一台主机上可以同时运行数千个 Docker 容器

容器 除了运行其中应用外,基本不消耗额外的系统资源,使得应用的性能很高,同时系统的开销尽量小。传统 虚拟机 方式运行 10 个不同的应用就要起 10 个 虚拟机,而 Docker 只需要启动 10 个隔离的应用即可。

更多 Docker 相关教程见以下内容

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Ubuntu 14.04 安装 Docker  http://www.linuxidc.com/linux/2014-08/105656.htm 

Ubuntu 使用 VNC 运行基于 Docker 的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm 

Ubuntu 15.04 下安装 Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm 

在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm 

在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

具体说来,Docker 在如下几个方面具有较大的优势。

1) 更快速的交付和部署

对开发和运维(devop)人员来说,最希望的就是一次创建或配置,可以在任意地方正常运行。

开发者可以使用一个标准的镜像来构建一套开发 容器 ,开发完成之后,运维人员可以直接使用这个 容器 来部署代码。Docker 可以快速创建 容器 ,快速迭代应用程序,并让整个过程全程可见,使团队中的其他成员更容易理解应用程序是如何创建和工作的。Docker 容器 很轻很快!容器 的启动时间是秒级的,大量地节约开发、测试、部署的时间。

2) 更高效的虚拟化

Docker 容器 的运行不需要额外的 hypervisor 支持,它是内核级的虚拟化,因此可以实现更高的性能和效率。

3) 更轻松的迁移和扩展

Docker 容器 几乎可以在任意的平台上运行,包括物理机、虚拟机、公有云、私有云、个人电脑、服务器等。这种兼容性可以让用户把一个应用程序从一个平台直接迁移到另外一个。

4) 更简单的管理

使用 Docker,只需要小小的修改,就可以替代以往大量的更新工作。所有的修改都以增量的方式被分发和更新,从而实现自动化并且高效的管理。

5) 对比传统 虚拟机 总结

特性
容器
虚拟机
启动 秒级 分钟级
硬盘使用 一般为 MB 一般为 GB
性能 接近原生 弱于
系统支持量 单机支持上千个 容器 一般几十个

Docker 安装(Ubuntu 14.04)

1) 通过系统自带包安装

Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装。

sudo apt-get update
sudo apt-get install -y docker.io
sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

如果使用操作系统自带包安装 Docker,目前安装的版本是比较旧的 1.0.0。要安装更新的版本,可以通过使用 Docker 源的方式。之后要讲到的 Docker 加速器需要 1.3.2 版本以上,所以建议直接使用 Docker 源的方式安装最新版本。

2) 通过 Docker 源安装最新版本

要安装最新的 Docker 版本,首先需要安装 apt-transport-https 支持,之后通过添加源来安装。

sudo apt-get install apt-transport-https
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
sudo bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install lxc-docker

Docker 的 安装资源文件 存放在 Amazon S3,会间歇性连接失败。所以安装 Docker 的时候,会比较慢。你可以通过执行下面的命令,高速安装 Docker。

curl -sSL https://get.daocloud.io/docker | sh

适用于 Ubuntu,Debian 等大部分 Linux,会 3 小时同步一次 Docker 官方资源。如果安装不成功,可以选择使用二进制包安装方式。下载二进制包

Docker 的三个基本概念

1) 镜像(Image)

Docker 镜像就是一个只读的模板。

例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了 Apache 或用户需要的其它应用程序。

镜像可以用来创建 Docker 容器。Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。

2) 容器(Container)

Docker 利用 容器 来运行应用。

容器 是从镜像创建的运行实例。它可以被启动、开始、停止、删除。每个 容器 都是相互隔离的、保证安全的平台。可以把 容器 看做是一个简易版的 Linux 环境(包括 root 用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。

* 注:镜像是只读的,容器 在启动的时候创建一层可写层作为最上层。

3) 仓库(Repository)

仓库是集中存放镜像文件的场所。有时候会把仓库和仓库注册服务器 (Registry) 混为一谈,并不严格区分。实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)。

仓库分为公开仓库(Public)和私有仓库(Private)两种形式。最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。当然,用户也可以在本地网络内创建一个私有仓库。当用户创建了自己的镜像之后就可以使用 push 命令将它上传到公有或者私有仓库,这样下次在另外一台机器上使用这个镜像时候,只需要从仓库上 pull 下来就可以了。

* 注:Docker 仓库的概念跟 Git 类似,注册服务器可以理解为 GitHub 这样的托管服务。

Docker 加速器

由于国内网络问题的限制,你要从 Docker Hub 上下载一个镜像,非常慢,可能要等半天。国内就有公司 (DaoCloud) 推出了 Docker 加速器(即 Docker Hub Mirror 服务),要使用该加速器,需要去 DaoCloud 注册一个帐号,得到相应的加速器地址。配置 Docker 加速器命令如下:

echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-mirror=http://×××××.m.daocloud.io\"" | sudo tee -a /etc/default/docker
sudo service docker restart

该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/default/docker 中。适用于 Ubuntu14.04,其他版本可能有细微不同。其中×××××需要替换为你自己的地址。

尝试去拉镜像会发现速度变快很多:

docker pull ubuntu:14.04

Mirror 是 Docker 的官方机制,它是 Registry 的一种特殊类型,在部署了 Registry 之后,需要开启 Mirror 模式并做一定的配置。具体的流程如下:

Docker 学习笔记

Docker Hub 由 Index 和 Registry 构成,Index 保存 Image Layer 的 hash 和关联关系等元数据(Metadata),Registry 用于存储 Image Layer 的实际二进制数据。在客户端没有配置 registry-mirror 参数的情况下,每一次 docker pull,客户端都会先连接 Index 获取元数据,然后再连接 Registry 获取实际的 Image 文件。由于 Docker Hub 的 Index 节点和 Regsitry 都部署国外,国内用户访问,经常遭遇连接超时或中断的情况,下载速度也极其缓慢。在启用了 Mirror 之后,访问流程如下:

  • 客户端的 Docker Daemon 连接 Index 获取 Metadata,这一部分的数据量极小,直连国外的速度可以忍受。
  • 根据 Metadata 的信息,Docker Daemon 与 Mirror 服务器建立连接。如果 pull 的 Image 在 Mirror 上已经有缓存,就直接在 Mirror 上返回地址并下载。
  • 如果 Image 在 Mirror 并无缓存,Mirror 会与 Docker Hub Registry 建立连接,下载 Image,提供给用户的同时,在本地缓存。
  • Mirror 下载 Docker Hub Image 采用 stream 的方式,即可以一边下载,一边提供给客户端的 Docker Daemon,不必等 Image 完全下载完。

通过以上的描述,可以发现,对于常用的 Image,Mirror 缓存命中率会非常高,如 Ubuntu 等基础 Image,这会极大提高下载速度。同时,Docker Image 采用分层的结构,即使 Image 被更新,也只是下载最新一层非常少的增量数据。

Mirror 服务亦可以通过网络优化,加速对远端 Docker Hub Registry 的访问速度,如采用高速的商业 VPN 建立从 Mirror 到 Docker Hub Registry 的访问。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-10/124483p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计20058字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中