简体   繁体   English

Docker 镜像/容器里面有什么?

[英]What's inside a Docker image/container?

Considering the fact that docker images/containers come in various flavours - Ubuntu, CentOS, CoreOS etc.... I'm curious what actually makes up an image/container, and what is shared with the host OS?考虑到 docker 图像/容器有多种风格——Ubuntu、CentOS、CoreOS 等……我很好奇图像/容器的实际组成以及与主机操作系统共享的内容? Where is the dividing line?分界线在哪里?

For example, I can download the base Ubuntu image and launch it on a CentOS host.例如,我可以下载基本的 Ubuntu 映像并在 CentOS 主机上启动它。 Then, when I poke around inside the Ubuntu container I can see that it looks and feels like an Ubuntu server (filesystem layout etc).然后,当我在 Ubuntu 容器内部闲逛时,我可以看到它的外观和感觉就像一个 Ubuntu 服务器(文件系统布局等)。 But if I run a uname command I see the kernel and the likes of the CentOS host....但是如果我运行一个 uname 命令,我会看到内核和 CentOS 主机之类的东西....

Obviously I understand that the underlying kernel is shared by all containers on the same host.显然我理解底层内核由同一主机上的所有容器共享。 But what else is shared with the host OS, and what is part of the image/container?但是还有什么是与主机操作系统共享的,镜像/容器的一部分是什么?

Eg the kernel is part of the host, the filesystem layout is part of the the image/container.... Is there a spec that defines this?例如,内核是主机的一部分,文件系统布局是图像/容器的一部分......是否有定义这一点的规范?

It can be helpful to distinguish between images and containers ( docs ).区分图像容器( docs ) 会很有帮助。 An image is static and lives only on disk.图像是静态的,只存在于磁盘上。 A container is a running instance of an image and it includes its own process tree as well as RAM and other runtime resources.容器映像的运行实例,它包括自己的进程树以及 RAM 和其他运行时资源。

An image is a logical grouping of layers plus metadata about what to do when creating a container and how to assemble the layers.图像是层的逻辑分组以及有关在创建容器时要做什么以及如何组装层的元数据。 Part of that metadata is that each layer knows its parent's ID.该元数据的一部分是每个层都知道其父级的 ID。

So, what goes into a layer?那么,什么进入层? The files (and directories) you've added to the parent.您添加到父级的文件(和目录)。 There are also special files ("whiteout") that indicate that something was deleted from the parent.还有一些特殊文件(“whiteout”)表明某些内容已从父级中删除。

When you docker run an image, docker creates a container: it unpacks all the layers in the correct order, creating a new "root" file system separate from the host.当您使用docker run一个镜像时, docker会创建一个容器:它以正确的顺序解压所有层,创建一个与主机分离的新“根”文件系统。 docker also reads the image metadata and starts either the "entrypoint" or "command" specified when the image was created -- that starts a new process sub-tree. docker还会读取图像元数据并启动创建图像时指定的“入口点”或“命令”——这将启动一个新的进程子树。 From inside the container, that first process seems like the root of the tree, but from the host you can see it is a subtree of processes.从容器内部看,第一个进程似乎是树的根,但从主机上可以看到它是进程的子树。

The root file system is what makes one Linux distro different from another (there can be some kernel module differences as well, and bootloader/boot file system differences, but these are usually invisible to the running processes).根文件系统使 Linux 发行版与另一个发行版不同(也可能存在一些内核模块差异,以及引导加载程序/引导文件系统差异,但这些通常对正在运行的进程不可见)。 The kernel is shared with the host and is, in fact, still managing its usual responsibilities inside the container.内核与主机共享,实际上,它仍在容器内管理其通常的职责。 But the root file system is different, and so when you're inside the container, it looks and feels like whatever distro was in the Docker image.但是根文件系统是不同的,所以当你在容器中时,它的外观和感觉就像 Docker 镜像中的任何发行版。

The container not only has its own file system and process tree, but also has its own logical network interface and, optionally, its own allocation of RAM and CPU time.容器不仅拥有自己的文件系统和进程树,还拥有自己的逻辑网络接口,以及自己的 RAM 和 CPU 时间分配(可选)。 You're in control over the container though, as the operator, so you can decide to share the host's network interface with the container, give it unlimited access to RAM and CPU, and even mount devices, files and directories from the host into the container.但是,作为操作员,您可以控制容器,因此您可以决定与容器共享主机的网络接口,授予它对 RAM 和 CPU 的无限制访问权限,甚至将设备、文件和目录从主机挂载到容器中容器。 The default is to keep things separate, but you have the power to break the isolation model as much as you need to.默认设置是将事物分开,但您可以根据需要尽可能地打破隔离模型。

Docker is a wrapper over LXC Linux Containers and the documentation for that will let you know in detail what is shared and what is not. Docker 是LXC Linux Containers 的包装器,其文档将让您详细了解哪些是共享的,哪些不是。

In general the host machine sees/contains everything inside the containers from file system to processes etc. You can issue a ps command on the host vm and see processes inside the container.通常,主机可以查看/包含容器内从文件系统到进程等的所有内容。您可以在主机 vm 上发出 ps 命令并查看容器内的进程。

Remember docker containers are not VMs - hence everything is actually running natively on the host and is using the host kernel directly.请记住,docker 容器不是虚拟机——因此一切实际上都是在主机上本地运行的,并且直接使用主机内核。 Each container has its own user namespace (similar to root jails of old).每个容器都有自己的用户命名空间(类似于旧的根监狱)。 There are tools/features which make sure containers only see its own processes, have its own file-system layered onto host file system, and a networking stack which pipes to the host networking stack.有一些工具/功能可以确保容器只看到它自己的进程,有自己的文件系统分层到主机文件系统,以及一个网络堆栈,它通过管道连接到主机网络堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM