Docker是一个用于开发、发布和运行应用程序的开放平台。

基于Linux cgroups技术进行CPU、内存资源管理,namespace技术进行主机名、文件和网络隔离。

入门

Debian安装方法: https://docs.docker.com/engine/install/debian/#install-using-the-repository

运行hello-world

root@ams-advin:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:62af9efd515a25f84961b70f973a798d2eca956b1b2b026d0a4a63a3b0b6a3f2
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.
    (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/get-started/

Linux的chroot用来更改进程的根目录。可以隔离进程能够放访问的文件,但无法隔离网络内存等资源。

Docker使用以下五种Namespace技术来隔离内核资源:

  1. pid,隔离进程ID
  2. net,隔离网络接口
  3. mnt,隔离文件挂载点
  4. ipc,隔离信号量,消息队列和共享内存
  5. uts,隔离主机名和域名

Docker使用Cgroup来限制进程的资源使用:CPU,内存,磁盘IO,网络IO等
Docker使用联合文件系统来实现容器的写时复制,镜像的分层构建和存储。常用联合文件系统有AUFS、Overlay和Devicemapper.

镜像,是一个只读文件,用于存放容器启动所需文件。
容器,是镜像运行的一个实例,一个镜像可以建立多个容器。
镜像仓库,用于存放和分发Docker镜像。

Open Container Initiative 开放容器标准,规定了容器的运行时标准和镜像标准。

Docker架构.png

镜像

docker pull 获取镜像
docker tag 重命名
docker image is 查看镜像
docker rmi 删除
docker run 启动容器
docker commit 基于容器构建镜像
docker build 基于Dockerfile构建镜像

获取镜像

docker pull [仓库服务器地址,默认docker.io]/[仓库名称]/[镜像名称]:[镜像标签,默认latest]

root@ams-advin:~# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
729ce43e2c91: Pull complete 
Digest: sha256:ad9bd57a3a57cc95515c537b89aaa69d83a6df54c4050fcf2b41ad367bec0cd5
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest

重命名

docker tag 源镜像 目标镜像

root@ams-advin:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
busybox       latest    2bd29714875d   2 weeks ago     1.24MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB
root@ams-advin:~# docker tag busybox:latest mybb:newest
root@ams-advin:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
busybox       latest    2bd29714875d   2 weeks ago     1.24MB
mybb          newest    2bd29714875d   2 weeks ago     1.24MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB
root@ams-advin:~# docker image rm mybb:newest
Untagged: mybb:newest
root@ams-advin:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
busybox       latest    2bd29714875d   2 weeks ago     1.24MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB

构建镜像

docker commit 将运行的容器存为镜像
docker build 从Dockerfile构建镜像

  • 将容器存为镜像
    docker commid 容器名 新的镜像名:镜像标签
  • 从Dockerfile构建
    Dockerfile是纯文本文件。
    Dockerfile每行命令都会生成一个独立的镜像层,拥有唯一的ID。

    Dockerfile指令指令简介
    FROMDockerfile除了注释第一行必须是FROM,FROM后面跟镜像名称,代表我们要基于哪个基础镜像构建我们的容器
    RUNRUN后面跟一个具体的命令,类似于Linux命令行执行命令
    ADD拷贝本机文件或者远程文件到镜像内
    COPY拷贝本机文件到镜像内
    USER指定容器启动的用户
    ENTRYPOINT容器的启动命令
    CMDCMD为ENTRYPOINT指令提供默认参数,也可以单独使用CMD指定容器启动参数
    ENV指定容器运行时的环境变量,格式为key=value
    ARG定义外部变量,构建镜像时可以使用build-arg=的格式传递参数用于构建
    EXPOSE指定容器监听的端口,格式为[port]/tcp或者[port]/udp
    WORKDIR为Dockerfile中跟在其后的所有RUN、CMD、ENTRYPOINT、COPY和ADD命令设置工作目录
文章目录