moby/moby
Daemon core
Purpose
daemon/ is where most of the codebase lives. It implements container lifecycle (create, start, stop, kill, exec, attach, logs, stats, prune, commit, export), exposes the backend interfaces that the API server depends on, and ties together every other subsystem (image, libnetwork, volumes, plugins, swarm, builder).
The Daemon struct
The central type is Daemon in daemon/daemon.go. It is ~1900 lines of fields, constructors, and lifecycle methods; the full struct definition starts at the top of the file and continues for 100+ fields. Highlights:
| Field | Type | Notes |
|---|---|---|
containers |
container.Store |
Source-of-truth in-memory container map. Backed by JSON on disk. |
containersReplica |
container.ViewDB |
Read-optimized snapshot; uses hashicorp/go-memdb. |
imageService |
ImageService |
Containerd-backed or graphdriver-backed (chosen at startup). |
netController |
*libnetwork.Controller |
Owns CNM networks, sandboxes, endpoints. |
volumes |
*volumesservice.VolumesService |
Volume API surface and mount tracking. |
containerdClient |
*containerd.Client |
gRPC client for the upstream containerd daemon. |
containerd |
libcontainerdtypes.Client |
Moby's wrapper around the containerd client. |
pluginManager |
*plugin.Manager |
Managed (v2) plugin manager. |
PluginStore |
*plugin.Store |
Legacy v1 plugin store (TODO: remove). |
cluster, clusterProvider |
Cluster, cluster.Provider |
Swarm-mode plumbing. |
EventsService |
*events.Events |
Topic-based event bus. |
statsCollector |
*stats.Collector |
Aggregates per-container cgroup stats. |
attachmentStore |
network.AttachmentStore |
Tracks swarm attachable network attachments. |
A few singleflight groups (resenje.org/singleflight) deduplicate expensive disk-usage queries (usageContainers, usageImages, usageVolumes, usageLayer).
Container lifecycle
Each container action has its own file at the package root, all on the *Daemon receiver:
| Action | File |
|---|---|
| Create | create.go, create_unix.go, create_windows.go |
| Start | start.go, start_unix.go, start_windows.go |
| Stop | stop.go |
| Kill | kill.go |
| Pause / unpause | pause.go, unpause.go |
| Restart | restart.go |
| Delete | delete.go |
| Exec | exec.go, exec_linux.go |
| Attach | attach.go |
| Logs | logs.go |
| Stats | stats.go, stats_unix.go, stats_windows.go |
| Inspect | inspect.go |
| List | list.go |
| Prune | prune.go |
| Commit | commit.go |
| Export / archive | export.go, archive.go |
| Update | update.go |
| Wait | wait.go |
| Resize | resize.go |
| Top | top_unix.go, top_windows.go |
OCI spec generation
When starting a container, the daemon turns the API request + image config + host config + libnetwork sandbox into an OCI runtime spec. The Linux path is daemon/oci_linux.go (~34K LOC) and the Windows path is daemon/oci_windows.go. Helpers live in daemon/oci_opts.go and daemon/oci_utils.go.
Container monitor
When a container starts, the daemon spawns a monitor goroutine in daemon/monitor.go. It waits on the containerd task, restarts the container per its restart policy (using daemon/internal/restartmanager/), records exit info, and emits events.
Storage layout
Daemon.repository (the --data-root directory, default /var/lib/docker) contains:
containers/<id>/— config, mounts, hostname/hosts/resolv.conf, log files.image/,overlay2/, etc. — graphdriver/snapshotter state.network/— libnetwork persistent state.volumes/— local volume root.swarm/— SwarmKit raft state when joined to a cluster.plugins/— managed plugin state.
Reload
SIGHUP triggers Daemon.Reload (daemon/reload.go). Only a curated set of fields can change at runtime (debug, log-level, labels, registry-mirrors, insecure-registries, runtimes). The rest require a restart.
Migration
The transition from the legacy graphdriver-backed image store to the containerd-backed store is implemented in daemon/migration.go and daemon/containerd/migration/. It walks layer metadata in image/ and writes equivalent records into containerd.
Entry points for modification
- Add a new container action: implement on
*Daemon, expose via the right backend interface indaemon/server/backend/, mount indaemon/server/router/container/. - Touching startup order: see
NewDaemonindaemon/daemon.go. - Reload semantics:
Daemon.Reloadindaemon/reload.go. - The roadmap calls out internal decoupling — patches that reduce the surface of
Daemonare welcome.
See also
- API server for how routers reach
Daemon. - Container runtime for how
Daemontalks to containerd. - Image management for the two
ImageServiceimplementations.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.