2014年05月12日

Docker之文件组织结构分析


本文分析docker在主机上的文件组织情况

背景

本文中涉及的源码、测试环境都是基于docker 0.11.1版本系统和docker-registry 0.6.5版本系统。

存储位置

docker在主机上默认的文件保存位置为/var/lib/docker

drwxr-xr-x   2 root root  4096 May 20 11:13 apparmor/
drwxr-xr-x   5 root root  4096 Jan 19 09:28 aufs/
drwx------ 140 root root 20480 May 16 16:44 containers/
drwx------   3 root root  4096 Jan 19 09:28 execdriver/
drwx------ 186 root root 20480 May 16 11:39 graph/
drwx------   2 root root  4096 May 16 15:47 init/
-rw-r--r--   1 root root 52224 May 16 16:44 linkgraph.db
-rw-------   1 root root  4720 May 16 11:44 repositories-aufs
drwx------   3 root root  4096 Apr 29 16:50 vfs/
drwx------ 100 root root 12288 May  9 12:49 volumes/

aufs

aufs目录中有三个目录,diff、layers、mnt。

diff中保存着每个镜像的layer文件解压后的结果。

layers中保存每个镜像的继承关系。

mnt目录中保存容器运行的rootfs。

containers

containers目录中保存每个容器的配置文件、环境变量文件以及日志文件。

以下是containers目录中一个容器目录中的文件内容。

drwx------   2 root root  4096 May 16 16:58 ./
drwx------ 140 root root 20480 May 16 16:44 ../
-rw-------   1 root root  4567 May 16 15:48 84d63434aaf9d94bce248b88743a20d7b8d55b55cc7e7643b68cc6f9e8257e02-json.log
-rw-r--r--   1 root root   102 May 14 15:21 config.env
-rw-r--r--   1 root root  1557 May 16 15:47 config.json
-rw-r--r--   1 root root   291 May 16 15:47 hostconfig.json
-rw-r--r--   1 root root    13 May 16 15:47 hostname
-rw-r--r--   1 root root   174 May 16 15:47 hosts
-rw-r--r--   1 root root    20 May 14 15:21 resolv.conf

退出一个容器后,容器的目录仍然保留。只有通过docker rm 命令删除容器后,containers目录中相应容器的目录才会被删除。

execdriver

运行中的容器会在execdriver/native下有一个相应的目录,包含container.json、pid、start三个文件。

container.json是容器的描述文件。

{
    "hostname":"84d63434aaf9",
    "environment":[
            "HOME=/","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "HOSTNAME=84d63434aaf9"
    ],
    "namespaces":{
            "NEWIPC":true,
            "NEWNET":true,
            "NEWNS":true,
            "NEWPID":true,
            "NEWUTS":true
    },
    "capabilities_mask":{
        "AUDIT_CONTROL":false,
        "AUDIT_WRITE":false,
        "MAC_ADMIN":false,
        "MAC_OVERRIDE":false,
        "MKNOD":true,
        "NET_ADMIN":false,
        "SETPCAP":false,
        "SYSLOG":false,
        "SYS_ADMIN":false,
        "SYS_MODULE":false,
        "SYS_NICE":false,
        "SYS_PACCT":false,
        "SYS_RAWIO":false,
        "SYS_RESOURCE":false,
        "SYS_TIME":false,
        "SYS_TTY_CONFIG":false
    },
    "networks":[
        {
            "type":"loopback",
            "address":"127.0.0.1/0",
            "gateway":"localhost",
            "mtu":1500
        },
        {
            "type":"veth",
            "context":{"bridge":"docker0","prefix":"veth"},
            "address":"172.17.0.3/16",
            "gateway":"172.17.42.1",
            "mtu":1500}
    ],
    "cgroups":{"name":"84d63434aaf9d94bce248b88743a20d7b8d55b55cc7e7643b68cc6f9e8257e02","parent":"docker"},
    "context":{
        "apparmor_profile":"docker-default",
        "mount_label":"",
        "process_label":"",
        "restrictions":"true"
    },
    "mounts":[
        {"type":"devtmpfs"},
        {"type":"bind","source":"/var/lib/docker/init/dockerinit-0.11.1","destination":"/.dockerinit","private":true},
        {"type":"bind","source":"/var/lib/docker/containers/84d63434aaf9d94bce248b88743a20d7b8d55b55cc7e7643b68cc6f9e8257e02/resolv.conf","destination":"/etc/resolv.conf","private":true},
        {"type":"bind","source":"/var/lib/docker/containers/84d63434aaf9d94bce248b88743a20d7b8d55b55cc7e7643b68cc6f9e8257e02/hostname","destination":"/etc/hostname","private":true},
        {"type":"bind","source":"/var/lib/docker/containers/84d63434aaf9d94bce248b88743a20d7b8d55b55cc7e7643b68cc6f9e8257e02/hosts","destination":"/etc/hosts","private":true}
    ]

pid文件中保存着容器的第一个进程id。

start文件中的内容尚不清楚,需要后续通过阅读代码来确定。

graph

graph目录保存镜像信息,每个镜像有两个描述文件。

json

json文件保存镜像描述

{
    "id":"8aedcdadf14bb95a53f712aace7d0cb657447d24e41831518392d1a2b05d8bf7",
    "parent":"99ec81b80c55d906afd8179560fdab0ee93e32c52053816ca1d531597c1ff48f",
    "created":"2014-05-15T02:37:51.21802123Z",
    "container":"1d4f82ac317735ab5d51853fc5d61346351b79fb41e9e961172e45f0dba31e75",
    "container_config":{
        "Hostname":"1d4f82ac3177",
        "Domainname":"",
        "User":"",
        "Memory":0,
        "MemorySwap":0,
        "CpuShares":0,
        "AttachStdin":true,
        "AttachStdout":true,
        "AttachStderr":true,
        "PortSpecs":null,
        "ExposedPorts":null,
        "Tty":true,
        "OpenStdin":true,
        "StdinOnce":true,
        "Env":[
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd":["/bin/bash"],
        "Image":"99ec81b80c55",
        "Volumes":null,
        "WorkingDir":"",
        "Entrypoint":null,
        "NetworkDisabled":false,
        "OnBuild":null
    },
    "docker_version":"0.10.0",
    "config":{
        "Hostname":"",
        "Domainname":"",
        "User":"",
        "Memory":0,
        "MemorySwap":0,
        "CpuShares":0,
        "AttachStdin":false,
        "AttachStdout":false,
        "AttachStderr":false,
        "PortSpecs":null,
        "ExposedPorts":null,
        "Tty":true,
        "OpenStdin":true,
        "StdinOnce":true,
        "Env":[
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd":["/bin/bash"],
        "Image":"",
        "Volumes":null,
        "WorkingDir":"",
        "Entrypoint":null,
        "NetworkDisabled":false,
        "OnBuild":null},
        "architecture":"amd64",
        "os":"linux",
        "Size":43197746}

layersize

layersize文件保存镜像大小

init

init目标保存各个版本docker的初始化程序

linkgraph.db

linkgraph.db是一个sqlite3数据库文件,里面有两个表edge和entity。用于记录容器的链接关系。

sqlite> .schema
CREATE TABLE entity (
        id text NOT NULL PRIMARY KEY
    );
CREATE TABLE edge (
        "entity_id" text NOT NULL,
        "parent_id" text NULL,
        "name" text NOT NULL,
        CONSTRAINT "parent_fk" FOREIGN KEY ("parent_id") REFERENCES "entity" ("id"),
        CONSTRAINT "entity_fk" FOREIGN KEY ("entity_id") REFERENCES "entity" ("id")
        );
CREATE UNIQUE INDEX "name_parent_ix" ON "edge" (parent_id, name);

repositories-aufs

repositories-aufs记录docker中的镜像信息。

{
    "Repositories":{
        "10.1.1.64:5000/ubuntu":{
            "12.10":"f030997216f3f605c3db2e60fac9a327910f30319b3fae95a457f87801bc2734",
            "13.04":"514b348987eff7991ad24b3fa26838f9768f41a618a1f78c8a4e6d5382b14df9",
            "13.10":"7c0d7c261905a9fe83d1aea4037a265058f47e1e3e1134daac879393741cc016",
            "14.04":"8aedcdadf14bb95a53f712aace7d0cb657447d24e41831518392d1a2b05d8bf7",
            "latest":"8aedcdadf14bb95a53f712aace7d0cb657447d24e41831518392d1a2b05d8bf7",
            "quantal":"f030997216f3f605c3db2e60fac9a327910f30319b3fae95a457f87801bc2734",
            "raring":"514b348987eff7991ad24b3fa26838f9768f41a618a1f78c8a4e6d5382b14df9",
            "saucy":"7c0d7c261905a9fe83d1aea4037a265058f47e1e3e1134daac879393741cc016",
            "trusty":"8aedcdadf14bb95a53f712aace7d0cb657447d24e41831518392d1a2b05d8bf7"
        }
    }
}

volumes

此目录作用暂时不清楚。