Open-Source Wikis

/

Moby

/

Primitives

/

Container

moby/moby

Container

Definition

The runtime container object lives in daemon/container/. It bundles together what the daemon needs to manage a single container: config, state, mounts, network attachments, exec sessions, log driver instance, and locks.

Two types of "container" coexist:

Daemon-side fields (selected)

Field Source
ID, Name daemon/container/container.go
Created, Path, Args from API request
Config *containertypes.Config, HostConfig *containertypes.HostConfig API config
State *State live state machine (state.go)
RWLayer layer.RWLayer the writable layer (legacy store)
MountPoints map[string]*MountPoint resolved mounts (mounts.go)
NetworkSettings *network.Settings runtime network attachments
LogDriver logger.Logger active log driver instance
ExecCommands *ExecStore live exec sessions (exec.go)
RestartManager *restartmanager.RestartManager restart policy state machine
attachContext attach lifecycle bookkeeping

Per-OS files extend this with platform-specific bits:

Container store

store.go defines an in-memory Store that maps ID → container. daemon/daemon.go also keeps a separate read-optimized snapshot (ViewDB, view.go) backed by hashicorp/go-memdb. Reads (docker ps) hit the view; writes go through Store and trigger a view rebuild.

Persistence

Each container's state is persisted under <data-root>/containers/<id>/:

  • config.v2.json — the daemon-side struct.
  • hostconfig.json — per-host runtime config.
  • hostname, hosts, resolv.conf — generated networking files.
  • mounts/, secrets/, configs/ — bind mount targets for swarm-managed bits.
  • The log file (when json-file or local).

On startup, the daemon walks this directory and reconstructs Store.

State machine

state.go tracks Running, Paused, Restarting, OOMKilled, Dead, Pid, ExitCode, Error, StartedAt, FinishedAt. The daemon and the container monitor goroutine update this state under a lock; Wait and WaitWithContext wake on transitions.

Exec

ExecStore holds an ExecConfig per exec session. ExecConfig mirrors api/types/container/exec.go and adds the live containerd process handle. Exec sessions live until the daemon notices the process has exited.

Health

Health (api/types/container/health.go) holds Status, FailingStreak, and a ring buffer of recent log entries. The healthchecker is implemented in daemon/health.go and runs as a goroutine per container.

How it interacts with the rest

graph LR
  Daemon -->|owns| Store
  Store --> Container
  Container -->|RWLayer| LayerStore
  Container -->|Sandbox| Libnetwork
  Container -->|Mounts| Volumes
  Container -->|LogDriver| Logger
  Container -->|task| Containerd

See also

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Container – Moby wiki | Factory