docker run流程
创建容器
通过用户指定的镜像名和tag,在TagStore中查找image_id,获取image对象(本地的image json文件里有镜像信息)
检查镜像layer数(不超过127),镜像层数太多会造成性能问题
将runconfig.Config和image.Config合并
创建{Container.ID}-init和Container.ID镜像的目录,在init layer创建.dockerinit、/etc/hosts、/etc/hostname等, Container.ID镜像是read-write layer
将Container对象持久化到path.Join(Container.ID, “config.json”)
在Daemon.idIndex中注册Container.ID,在Daemon.contStore中记录Container.ID和Container对象的映射关系
启动容器
通过Container.ID从Daemon.contStore中获取Container对象
配置容器的dns地址/etc/resolv.conf
将所有祖先镜像挂载到path.Join(graphdriver.root, “mnt”, Container.ID)
初始化容器的hosts文件/etc/hosts
检查主机是否开启cgroup内存限制、swap内存限制、ipv4转发
将容器内挂在目录和宿主机目录的映射关系存入container.Volume
将容器的link信息存入graphdb中,基于sqllite的图模型的数据库
通过iptables开启link容器间的通信,并env化
获取容器运行的当前目录,获取用户指定的环境变量
构建execdriver的Command对象,包括进程命令、Network、Mount、Resources、WorkingDir,用于容器的启动和配置
将Command对象持久化到path.Join(config.root, “execdriver/native/container.json”)
通过execdriver.Run启动Command对象