Skip to content

Modern Docker Management, Designed for Everyone

fix: containers fail to start due to cgroup oci error (#2039)

Kyle Mendellabab7d007df9
diff --git a/backend/cli/upgrade/doc.go b/backend/cli/upgrade/doc.go
new file mode 100644
index 000000000..384f2ba46
--- /dev/null
+++ b/backend/cli/upgrade/doc.go
@@ -0,0 +1,3 @@
+// Package upgrade provides the Arcane CLI command that upgrades a running
+// Arcane container by recreating it with a newer image.
+package upgrade
diff --git a/backend/cli/upgrade/upgrade.go b/backend/cli/upgrade/upgrade.go
index b4bbafa02..2fdb9373b 100644
--- a/backend/cli/upgrade/upgrade.go
+++ b/backend/cli/upgrade/upgrade.go
@@ -22,6 +22,8 @@ var (
autoDetect bool
)
+// UpgradeCmd recreates a running Arcane container with a newer image while
+// preserving its configuration.
var UpgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade an Arcane container to the latest version",
@@ -338,11 +340,26 @@ func upgradeContainer(ctx context.Context, dockerClient *client.Client, oldConta
config := *oldContainer.Config
config.Image = newImage
- hostConfig := oldContainer.HostConfig
+ hostConfig, sanitizedMemorySwappiness, engineInfo, err := libarcane.PrepareRecreateHostConfigForEngine(ctx, dockerClient, oldContainer.HostConfig)
+ if err != nil {
+ return fmt.Errorf("prepare host config: %w", err)
+ }
+ if sanitizedMemorySwappiness {
+ slog.Info("Stripped unsupported host config field for recreate",
+ "container", originalName,
+ "containerId", oldContainer.ID,
+ "engine", engineInfo.Name,
+ "cgroupVersion", engineInfo.CgroupVersion,
+ "field", "memorySwappiness",
+ )
+ }
// Fix for "conflicting options: hostname and the network mode"
// When network mode is "host" or "container:...", Hostname must be empty
- nm := hostConfig.NetworkMode
+ var nm container.NetworkMode
+ if hostConfig != nil {
+ nm = hostConfig.NetworkMode
+ }
if nm.IsHost() || nm.IsContainer() {
config.Hostname = ""
config.Domainname = ""
@@ -359,8 +376,10 @@ func upgradeContainer(ctx context.Context, dockerClient *client.Client, oldConta
// When network mode is "container:...", port mappings are not allowed
if nm.IsContainer() {
config.ExposedPorts = nil
- hostConfig.PortBindings = nil
- hostConfig.PublishAllPorts = false
+ if hostConfig != nil {
+ hostConfig.PortBindings = nil
+ hostConfig.PublishAllPorts = false
+ }
}
// Build network config - preserve all network settings including IP addresses
diff --git a/backend/internal/services/updater_service.go b/backend/internal/services/updater_service.go
index b04c62111..1d06966ee 100644
--- a/backend/internal/services/updater_service.go
+++ b/backend/internal/services/updater_service.go
@@ -26,6 +26,8 @@ import (
"github.com/getarcaneapp/arcane/types/updater"
)
+// UpdaterService coordinates image update checks, container recreation, and
+// project redeploy flows for Arcane's auto-update system.
type UpdaterService struct {
db *database.DB
settingsService *SettingsService
@@ -43,6 +45,8 @@ type UpdaterService struct {
updatingProjects map[string]bool
}
+// NewUpdaterService constructs an UpdaterService with the dependencies needed
+// to plan, execute, and record container and project updates.
func NewUpdaterService(
db *database.DB,
settings *SettingsService,
@@ -71,6 +75,10 @@ func NewUpdaterService(
}
}
+// ApplyPending executes the currently pending image updates and returns a
+// per-resource result summary. When dryRun is true, it reports the planned
+// actions without mutating containers or projects.
+//
//nolint:gocognit
func (s *UpdaterService) ApplyPending(ctx context.Context, dryRun bool) (*updater.Result, error) {
start := time.Now()
@@ -680,8 +688,10 @@ func (s *UpdaterService) pruneImageIDsWithInUseSetInternal(ctx context.Context,
return nil
}
+// GetStatus returns the current in-memory update activity snapshot.
func (s *UpdaterService) GetStatus() updater.Status { return s.statusSnapshotInternal() }
+// GetHistory returns the most recent auto-update history records, newest first.
func (s *UpdaterService) GetHistory(ctx context.Context, limit int) ([]models.AutoUpdateRecord, error) {
var rec []models.AutoUpdateRecord
q := s.db.WithContext(ctx).Order("start_time DESC")
@@ -744,9 +754,27 @@ func (s *UpdaterService) updateContainer(ctx context.Context, cnt container.Summ
cfg := inspect.Config
cfg.Image = newRef
+ hostConfig, sanitizedMemorySwappiness, engineInfo, err := libarcane.PrepareRecreateHostConfigForEngine(ctx, dcli, inspect.HostConfig)
+ if err != nil {
+ return fmt.Errorf("prepare host config: %w", err)
+ }
+ if sanitizedMemorySwappiness {
+ slog.InfoContext(ctx,
+ "updateContainer: stripped unsupported host config field for recreate",
+ "containerId", cnt.ID,
+ "containerName", name,
+ "engine", engineInfo.Name,
+ "cgroupVersion", engineInfo.CgroupVersion,
+ "field", "memorySwappiness",
+ )
+ }
+
// Fix for "conflicting options: hostname and the network mode"
// When network mode is "host" or "container:...", Hostname must be empty
- nm := inspect.HostConfig.NetworkMode
+ var nm container.NetworkMode
+ if hostConfig != nil {
+ nm = hostConfig.NetworkMode
+ }
if nm.IsHost() || nm.IsContainer() {
cfg.Hostname = ""
cfg.Domainname = ""
@@ -756,8 +784,10 @@ func (s *UpdaterService) updateContainer(ctx context.Context, cnt container.Summ
// When network mode is "container:...", port mappings are not allowed
if nm.IsContainer() {
cfg.ExposedPorts = nil
- inspect.HostConfig.PortBindings = nil
- inspect.HostConfig.PublishAllPorts = false
+ if hostConfig != nil {
+ hostConfig.PortBindings = nil
+ hostConfig.PublishAllPorts = false
+ }
}
apiVersion := libarcane.DetectDockerAPIVersion(ctx, dcli)
@@ -777,7 +807,7 @@ func (s *UpdaterService) updateContainer(ctx context.Context, cnt container.Summ
resp, err := dcli.ContainerCreate(ctx, client.ContainerCreateOptions{
Config: cfg,
- HostConfig: inspect.HostConfig,
+ HostConfig: hostConfig,
NetworkingConfig: networkingConfig,
Name: containerName,
})
diff --git a/backend/pkg/libarcane/doc.go b/backend/pkg/libarcane/doc.go
new file mode 100644
index 000000000..2f0aa5c61
--- /dev/null
+++ b/backend/pkg/libarcane/doc.go
@@ -0,0 +1,3 @@
+// Package libarcane contains shared compatibility helpers, setting utilities,
+// and internal resource metadata used across Arcane's backend and CLI.
+package libarcane
diff --git a/backend/pkg/libarcane/engine_compat.go b/backend/pkg/libarcane/engine_compat.go
new file mode 100644
index 000000000..5918ed4c5
--- /dev/null
+++ b/backend/pkg/libarcane/engine_compat.go
@@ -0,0 +1,127 @@
+package libarcane
+
+import (
+ "context"
+ "strings"
+
+ containertypes "github.com/moby/moby/api/types/container"
+ systemtypes "github.com/moby/moby/api/types/system"
+ "github.com/moby/moby/client"
+)
+
+// EngineCompatibilityInfo describes the container engine details Arcane uses to
+// decide whether recreate-time HostConfig sanitization is required.
+type EngineCompatibilityInfo struct {
+ // Name is the normalized engine identifier, such as "docker" or "podman".
+ Name string
+ // CgroupVersion is the daemon-reported cgroup version, such as "1" or "2".
+ CgroupVersion string
+}
+
+// PrepareRecreateHostConfigForEngine clones hostConfig and removes recreate
+// options that are known to be incompatible with the connected engine.
+//
+// The returned HostConfig is a shallow copy, so field reassignment is isolated
+// from the caller's original value, but content-level mutation of shared slice
+// or map fields is not. The boolean result reports whether the helper removed
+// any incompatible fields. EngineCompatibilityInfo reports the daemon details
+// used to make that decision.
+func PrepareRecreateHostConfigForEngine(ctx context.Context, dockerClient *client.Client, hostConfig *containertypes.HostConfig) (*containertypes.HostConfig, bool, EngineCompatibilityInfo, error) {
+ if hostConfig == nil {
+ return nil, false, EngineCompatibilityInfo{}, nil
+ }
+
+ cloned := cloneContainerHostConfigInternal(hostConfig)
+ if dockerClient == nil {
+ return cloned, false, EngineCompatibilityInfo{}, nil
+ }
+
+ serverVersion, err := dockerClient.ServerVersion(ctx, client.ServerVersionOptions{})
+ if err != nil {
+ return cloned, false, EngineCompatibilityInfo{}, err
+ }
+
+ infoResult, err := dockerClient.Info(ctx, client.InfoOptions{})
+ if err != nil {
+ return cloned, false, EngineCompatibilityInfo{}, err
+ }
+
+ engineInfo := detectEngineCompatibilityInfoInternal(serverVersion, infoResult.Info)
+ sanitized := sanitizeRecreateHostConfigInternal(cloned, engineInfo)
+
+ return cloned, sanitized, engineInfo, nil
+}
+
+func cloneContainerHostConfigInternal(hostConfig *containertypes.HostConfig) *containertypes.HostConfig {
+ if hostConfig == nil {
+ return nil
+ }
+
+ cloned := *hostConfig
+ return &cloned
+}
+
+func sanitizeRecreateHostConfigInternal(hostConfig *containertypes.HostConfig, engineInfo EngineCompatibilityInfo) bool {
+ if hostConfig == nil {
+ return false
+ }
+
+ if !isPodmanEngineInternal(engineInfo.Name) || !isCgroupV2Internal(engineInfo.CgroupVersion) {
+ return false
+ }
+
+ if hostConfig.MemorySwappiness == nil {
+ return false
+ }
+
+ hostConfig.MemorySwappiness = nil
+ return true
+}
+
+func detectEngineCompatibilityInfoInternal(version client.ServerVersionResult, info systemtypes.Info) EngineCompatibilityInfo {
+ return EngineCompatibilityInfo{
+ Name: detectEngineNameInternal(version, info),
+ CgroupVersion: strings.TrimSpace(info.CgroupVersion),
+ }
+}
+
+func detectEngineNameInternal(version client.ServerVersionResult, info systemtypes.Info) string {
+ candidates := []string{version.Platform.Name}
+ for _, component := range version.Components {
+ candidates = append(candidates, component.Name)
+ for _, value := range component.Details {
+ candidates = append(candidates, value)
+ }
+ }
+ candidates = append(candidates, info.ServerVersion, info.OperatingSystem)
+
+ for _, candidate := range candidates {
+ if name := normalizeEngineNameInternal(candidate); name != "" {
+ return name
+ }
+ }
+
+ return ""
+}
+
+func normalizeEngineNameInternal(value string) string {
+ normalized := strings.ToLower(strings.TrimSpace(value))
+ switch {
+ case strings.Contains(normalized, "podman"):
+ return "podman"
+ case strings.Contains(normalized, "docker"):
+ return "docker"
+ default:
+ return ""
+ }
+}
+
+func isPodmanEngineInternal(engineName string) bool {
+ return strings.EqualFold(strings.TrimSpace(engineName), "podman")
+}
+
+func isCgroupV2Internal(cgroupVersion string) bool {
+ normalized := strings.ToLower(strings.TrimSpace(cgroupVersion))
+ normalized = strings.TrimPrefix(normalized, "v")
+ return normalized == "2"
+}
diff --git a/backend/pkg/libarcane/engine_compat_test.go b/backend/pkg/libarcane/engine_compat_test.go
new file mode 100644
index 000000000..531a1eede
--- /dev/null
+++ b/backend/pkg/libarcane/engine_compat_test.go
@@ -0,0 +1,145 @@
+package libarcane
+
+import (
+ "testing"
+
+ containertypes "github.com/moby/moby/api/types/container"
+ systemtypes "github.com/moby/moby/api/types/system"
+ "github.com/moby/moby/client"
+ "github.com/stretchr/testify/require"
+)
+
+func TestPrepareRecreateHostConfigForEngine_NilHostConfig(t *testing.T) {
+ out, sanitized, engineInfo, err := PrepareRecreateHostConfigForEngine(t.Context(), nil, nil)
+ require.NoError(t, err)
+ require.Nil(t, out)
+ require.False(t, sanitized)
+ require.Empty(t, engineInfo.Name)
+ require.Empty(t, engineInfo.CgroupVersion)
+}
+
+func TestSanitizeRecreateHostConfigInternal(t *testing.T) {
+ swappiness := int64(60)
+
+ tests := []struct {
+ name string
+ engineInfo EngineCompatibilityInfo
+ wantSanitized bool
+ wantSwappinessNil bool
+ }{
+ {
+ name: "podman cgroup v2 strips memory swappiness",
+ engineInfo: EngineCompatibilityInfo{
+ Name: "podman",
+ CgroupVersion: "2",
+ },
+ wantSanitized: true,
+ wantSwappinessNil: true,
+ },
+ {
+ name: "podman cgroup v1 preserves memory swappiness",
+ engineInfo: EngineCompatibilityInfo{
+ Name: "podman",
+ CgroupVersion: "1",
+ },
+ wantSanitized: false,
+ wantSwappinessNil: false,
+ },
+ {
+ name: "docker cgroup v2 preserves memory swappiness",
+ engineInfo: EngineCompatibilityInfo{
+ Name: "docker",
+ CgroupVersion: "2",
+ },
+ wantSanitized: false,
+ wantSwappinessNil: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ input := &containertypes.HostConfig{
+ Resources: containertypes.Resources{
+ MemorySwappiness: &swappiness,
+ CPUShares: 1024,
+ },
+ PublishAllPorts: true,
+ ReadonlyRootfs: true,
+ NetworkMode: "bridge",
+ ContainerIDFile: "/tmp/id",
+ VolumeDriver: "local",
+ Annotations: map[string]string{"test": "value"},
+ MaskedPaths: []string{"/proc/kcore"},
+ ReadonlyPaths: []string{"/proc/asound"},
+ }
+
+ cloned := cloneContainerHostConfigInternal(input)
+ require.NotNil(t, cloned)
+ require.NotSame(t, input, cloned)
+
+ sanitized := sanitizeRecreateHostConfigInternal(cloned, tt.engineInfo)
+ require.Equal(t, tt.wantSanitized, sanitized)
+
+ if tt.wantSwappinessNil {
+ require.Nil(t, cloned.MemorySwappiness)
+ } else {
+ require.NotNil(t, cloned.MemorySwappiness)
+ require.Equal(t, int64(60), *cloned.MemorySwappiness)
+ }
+
+ require.NotNil(t, input.MemorySwappiness)
+ require.Equal(t, int64(60), *input.MemorySwappiness)
+ require.Equal(t, int64(1024), cloned.CPUShares)
+ require.True(t, cloned.PublishAllPorts)
+ require.True(t, cloned.ReadonlyRootfs)
+ require.Equal(t, containertypes.NetworkMode("bridge"), cloned.NetworkMode)
+ require.Equal(t, "/tmp/id", cloned.ContainerIDFile)
+ require.Equal(t, "local", cloned.VolumeDriver)
+ require.Equal(t, map[string]string{"test": "value"}, cloned.Annotations)
+ require.Equal(t, []string{"/proc/kcore"}, cloned.MaskedPaths)
+ require.Equal(t, []string{"/proc/asound"}, cloned.ReadonlyPaths)
+ })
+ }
+}
+
+func TestSanitizeRecreateHostConfigInternal_NilHostConfig(t *testing.T) {
+ sanitized := sanitizeRecreateHostConfigInternal(nil, EngineCompatibilityInfo{Name: "podman", CgroupVersion: "2"})
+ require.False(t, sanitized)
+}
+
+func TestDetectEngineCompatibilityInfoInternal(t *testing.T) {
+ t.Run("prefers platform name for podman detection", func(t *testing.T) {
+ version := client.ServerVersionResult{}
+ version.Platform.Name = "Podman Engine"
+ info := systemtypes.Info{CgroupVersion: "2"}
+
+ engineInfo := detectEngineCompatibilityInfoInternal(version, info)
+ require.Equal(t, "podman", engineInfo.Name)
+ require.Equal(t, "2", engineInfo.CgroupVersion)
+ })
+
+ t.Run("detects podman from component names", func(t *testing.T) {
+ version := client.ServerVersionResult{
+ Components: []systemtypes.ComponentVersion{
+ {Name: "Podman Engine"},
+ },
+ }
+ info := systemtypes.Info{CgroupVersion: "2"}
+
+ engineInfo := detectEngineCompatibilityInfoInternal(version, info)
+ require.Equal(t, "podman", engineInfo.Name)
+ require.Equal(t, "2", engineInfo.CgroupVersion)
+ })
+
+ t.Run("falls back to docker markers", func(t *testing.T) {
+ version := client.ServerVersionResult{}
+ info := systemtypes.Info{
+ CgroupVersion: "2",
+ ServerVersion: "Docker Engine - Community",
+ }
+
+ engineInfo := detectEngineCompatibilityInfoInternal(version, info)
+ require.Equal(t, "docker", engineInfo.Name)
+ require.Equal(t, "2", engineInfo.CgroupVersion)
+ })
+}