简体   繁体   English

docker ps 命令在哪里查找当前容器?

[英]Where does docker ps Command look for current containers?

I need to know how it works and how it finds the current container and where it looks for containers by default.我需要知道它是如何工作的以及它如何找到当前容器以及默认情况下它在哪里查找容器。

Don't get the wrong idea of the question.不要误解这个问题。 I am very familiar with the docker ps command.我对 docker ps 命令非常熟悉。

In need this in order to be able to ingest other containers in run time without the daemon noticing the difference.需要这样做是为了能够在运行时摄取其他容器而守护程序不会注意到差异。

TLDR; TLDR; the docker daemon keeps the list of containers in memory. docker 守护进程将容器列表保存在内存中。
On a docker ps , there is no filesystem access needed.docker ps ,不需要文件系统访问。
On a docker create/run/start , the list of container can be derived from the layer found in /var/lib/docker/aufs (if you are using the aufs storage driver )docker create/run/start ,可以从/var/lib/docker/aufs的层派生容器列表(如果您使用的是aufs 存储驱动程序

https://github.com/docker/docker/raw/83f28cb374f6e08e2e929ea86833c0976ce47cf3/docs/userguide/storagedriver/images/container-layers.jpg

The containers themselves are under /var/lib/docker/containers/{id}容器本身在/var/lib/docker/containers/{id}
As illsutrated by issue 15047 , that path is loaded when the docker daemon is started.问题 15047 所示,该路径在docker守护进程启动时加载。 After, this is managed in memory.之后,这是在内存中管理的。


docker ps asks for containers in api/client/ps.go#L59 : docker psapi/client/ps.go#L59请求容器:

containers, err := cli.client.ContainerList(options)

It asks api/client/lib/container_list.go#L45它询问api/client/lib/container_list.go#L45

resp, err := cli.get("/containers/json", query, nil)

That is managed in api/server/router/container/container.go#L34api/server/router/container/container.go#L34

local.NewGetRoute("/containers/json", r.getContainersJSON),

It then asks to the backend in api/server/router/container/container_routes.go#L48然后它在api/server/router/container/container_routes.go#L48询问后端

containers, err := s.backend.Containers(config)

Which is an interface in api/server/router/container/backend.go#L58这是api/server/router/container/backend.go#L58一个接口

Containers(config *daemon.ContainersConfig) ([]*types.Container, error)

implemented by the docker daemon in daemon/list.go#L83-L86 :daemon/list.go#L83-L86守护进程实现:

// Containers returns the list of containers to show given the user's filtering.
func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, error) {
    return daemon.reduceContainers(config, daemon.transformContainer)
}

The daemon has the list of containers: daemon/list.go#L37-L40守护进程有容器列表: daemon/list.go#L37-L40

// List returns an array of all containers registered in the daemon.
func (daemon *Daemon) List() []*container.Container {
    return daemon.containers.List()
}

From a contStore : daemon/daemon.go#L123-L132contStoredaemon/daemon.go#L123-L132

func (c *contStore) List() []*container.Container {
    containers := new(History)
    c.Lock()
    for _, cont := range c.s {
        containers.Add(cont)
    }
    c.Unlock()
    containers.sort()
    return *containers
}


type contStore struct {
    s map[string]*container.Container
    sync.Mutex
}

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

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