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
TLDR; the docker daemon keeps the list of containers in memory.
On a docker ps
, there is no filesystem access needed.
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)
The containers themselves are under /var/lib/docker/containers/{id}
As illsutrated by issue 15047, that path is loaded when the docker daemon is started. After, this is managed in memory.
docker ps asks for containers in api/client/ps.go#L59:
containers, err := cli.client.ContainerList(options)
It asks 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#L34
local.NewGetRoute("/containers/json", r.getContainersJSON),
It then asks to the backend in 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
Containers(config *daemon.ContainersConfig) ([]*types.Container, error)
implemented by the docker daemon in 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
// 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-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
}