From 47199c36ceaa760868e46a51c5caca9afdc9086b Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Wed, 22 Apr 2026 15:31:18 +1000 Subject: [PATCH] fix: containerd socket detection and unregistry startup on machine reboot --- internal/machine/machine.go | 54 ++++++++++++++++++++----------------- scripts/install.sh | 2 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/internal/machine/machine.go b/internal/machine/machine.go index f10a28f6..c9d0d954 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -104,27 +104,6 @@ func (c *Config) SetDefaults() (*Config, error) { } cfg.DockerClient = cli } - if cfg.ContainerdSockPath == "" { - // Auto-detect the containerd.sock path used by Docker. - paths := []string{ - "/run/containerd/containerd.sock", // Default path on most Linux distributions. - "/run/docker/containerd/containerd.sock", - "/var/run/containerd/containerd.sock", - "/var/run/docker/containerd/containerd.sock", - } - for _, path := range paths { - if _, err := os.Stat(path); err == nil { - cfg.ContainerdSockPath = path - slog.Debug("Detected containerd socket used by Docker.", "path", path) - break - } - } - - if cfg.ContainerdSockPath == "" { - slog.Warn("Failed to auto-detect containerd socket used by Docker.") - } - } - if cfg.CorrosionDir == "" { cfg.CorrosionDir = filepath.Join(cfg.DataDir, "corrosion") } @@ -340,6 +319,31 @@ func (m *Machine) Initialised() bool { return m.state.ID != "" } +// ContainerdSock returns the path to the containerd socket used by Docker, auto-discovering it from well-known +// locations if it's not explicitly configured. +// Returns an empty string if the socket cannot be detected. +func (m *Machine) ContainerdSock() string { + if m.config.ContainerdSockPath != "" { + return m.config.ContainerdSockPath + } + + paths := []string{ + "/run/containerd/containerd.sock", // Default path on most Linux distributions. + "/run/docker/containerd/containerd.sock", + "/var/run/containerd/containerd.sock", + "/var/run/docker/containerd/containerd.sock", + } + for _, path := range paths { + if _, err := os.Stat(path); err == nil { + slog.Debug("Detected containerd socket used by Docker.", "path", path) + return path + } + } + + slog.Warn("Failed to auto-detect containerd socket used by Docker.") + return "" +} + // IP returns the machine IPv4 address in the cluster network which is the first address in the machine subnet. func (m *Machine) IP() netip.Addr { if !m.Initialised() { @@ -460,7 +464,7 @@ func (m *Machine) Run(ctx context.Context) error { } var unreg *unregistry.Registry - if m.config.ContainerdSockPath != "" { + if containerdSock := m.ContainerdSock(); containerdSock != "" { isContainerdStore, err := m.dockerService.IsContainerdImageStoreEnabled(ctx) if err != nil { return fmt.Errorf("check if Docker uses containerd image store: %w", err) @@ -472,7 +476,7 @@ func (m *Machine) Run(ctx context.Context) error { unreg, err = unregistry.NewRegistry(unregistry.Config{ Addr: net.JoinHostPort(m.IP().String(), strconv.Itoa(constants.UnregistryPort)), ContainerdNamespace: "moby", - ContainerdSock: m.config.ContainerdSockPath, + ContainerdSock: containerdSock, LogFormatter: "text", LogLevel: "info", }) @@ -483,7 +487,7 @@ func (m *Machine) Run(ctx context.Context) error { slog.Warn("Skipping embedded unregistry setup as Docker is not using the containerd image store.") } } else { - slog.Warn("Skipping embedded unregistry setup as the containerd socket path is not configured.") + slog.Warn("Skipping embedded unregistry setup as the containerd socket path could not be detected.") } m.mu.Lock() @@ -1131,7 +1135,7 @@ func (m *Machine) InspectService( const logsHeartbeatInterval = 200 * time.Millisecond // MachineLogs streams logs from a systemd service. -func (s *Machine) MachineLogs( +func (m *Machine) MachineLogs( req *pb.LogsRequest, stream grpc.ServerStreamingServer[pb.LogEntry], ) error { // TODO(miek): almost duplicate of docker/server.ContainerLogs diff --git a/scripts/install.sh b/scripts/install.sh index 9e81e68a..9867ed92 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -204,7 +204,7 @@ install_uncloud_systemd() { cat > "${uncloud_service_path}" << EOF [Unit] Description=Uncloud machine daemon -After=network-online.target +After=network-online.target docker.service Wants=network-online.target [Service]