Skip to content

Commit 47199c3

Browse files
committed
fix: containerd socket detection and unregistry startup on machine reboot
1 parent d812cf8 commit 47199c3

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

‎internal/machine/machine.go‎

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,6 @@ func (c *Config) SetDefaults() (*Config, error) {
104104
}
105105
cfg.DockerClient = cli
106106
}
107-
if cfg.ContainerdSockPath == "" {
108-
// Auto-detect the containerd.sock path used by Docker.
109-
paths := []string{
110-
"/run/containerd/containerd.sock", // Default path on most Linux distributions.
111-
"/run/docker/containerd/containerd.sock",
112-
"/var/run/containerd/containerd.sock",
113-
"/var/run/docker/containerd/containerd.sock",
114-
}
115-
for _, path := range paths {
116-
if _, err := os.Stat(path); err == nil {
117-
cfg.ContainerdSockPath = path
118-
slog.Debug("Detected containerd socket used by Docker.", "path", path)
119-
break
120-
}
121-
}
122-
123-
if cfg.ContainerdSockPath == "" {
124-
slog.Warn("Failed to auto-detect containerd socket used by Docker.")
125-
}
126-
}
127-
128107
if cfg.CorrosionDir == "" {
129108
cfg.CorrosionDir = filepath.Join(cfg.DataDir, "corrosion")
130109
}
@@ -340,6 +319,31 @@ func (m *Machine) Initialised() bool {
340319
return m.state.ID != ""
341320
}
342321

322+
// ContainerdSock returns the path to the containerd socket used by Docker, auto-discovering it from well-known
323+
// locations if it's not explicitly configured.
324+
// Returns an empty string if the socket cannot be detected.
325+
func (m *Machine) ContainerdSock() string {
326+
if m.config.ContainerdSockPath != "" {
327+
return m.config.ContainerdSockPath
328+
}
329+
330+
paths := []string{
331+
"/run/containerd/containerd.sock", // Default path on most Linux distributions.
332+
"/run/docker/containerd/containerd.sock",
333+
"/var/run/containerd/containerd.sock",
334+
"/var/run/docker/containerd/containerd.sock",
335+
}
336+
for _, path := range paths {
337+
if _, err := os.Stat(path); err == nil {
338+
slog.Debug("Detected containerd socket used by Docker.", "path", path)
339+
return path
340+
}
341+
}
342+
343+
slog.Warn("Failed to auto-detect containerd socket used by Docker.")
344+
return ""
345+
}
346+
343347
// IP returns the machine IPv4 address in the cluster network which is the first address in the machine subnet.
344348
func (m *Machine) IP() netip.Addr {
345349
if !m.Initialised() {
@@ -460,7 +464,7 @@ func (m *Machine) Run(ctx context.Context) error {
460464
}
461465

462466
var unreg *unregistry.Registry
463-
if m.config.ContainerdSockPath != "" {
467+
if containerdSock := m.ContainerdSock(); containerdSock != "" {
464468
isContainerdStore, err := m.dockerService.IsContainerdImageStoreEnabled(ctx)
465469
if err != nil {
466470
return fmt.Errorf("check if Docker uses containerd image store: %w", err)
@@ -472,7 +476,7 @@ func (m *Machine) Run(ctx context.Context) error {
472476
unreg, err = unregistry.NewRegistry(unregistry.Config{
473477
Addr: net.JoinHostPort(m.IP().String(), strconv.Itoa(constants.UnregistryPort)),
474478
ContainerdNamespace: "moby",
475-
ContainerdSock: m.config.ContainerdSockPath,
479+
ContainerdSock: containerdSock,
476480
LogFormatter: "text",
477481
LogLevel: "info",
478482
})
@@ -483,7 +487,7 @@ func (m *Machine) Run(ctx context.Context) error {
483487
slog.Warn("Skipping embedded unregistry setup as Docker is not using the containerd image store.")
484488
}
485489
} else {
486-
slog.Warn("Skipping embedded unregistry setup as the containerd socket path is not configured.")
490+
slog.Warn("Skipping embedded unregistry setup as the containerd socket path could not be detected.")
487491
}
488492

489493
m.mu.Lock()
@@ -1131,7 +1135,7 @@ func (m *Machine) InspectService(
11311135
const logsHeartbeatInterval = 200 * time.Millisecond
11321136

11331137
// MachineLogs streams logs from a systemd service.
1134-
func (s *Machine) MachineLogs(
1138+
func (m *Machine) MachineLogs(
11351139
req *pb.LogsRequest, stream grpc.ServerStreamingServer[pb.LogEntry],
11361140
) error {
11371141
// TODO(miek): almost duplicate of docker/server.ContainerLogs

‎scripts/install.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ install_uncloud_systemd() {
204204
cat > "${uncloud_service_path}" << EOF
205205
[Unit]
206206
Description=Uncloud machine daemon
207-
After=network-online.target
207+
After=network-online.target docker.service
208208
Wants=network-online.target
209209
210210
[Service]

0 commit comments

Comments
 (0)