Skip to content

Modern Docker Management, Designed for Everyone

fix: preserve data volumes when recreating containers during upgrade (#4141)

Kyle Mendell75f6ddb2bb8c
diff --git a/Justfile b/Justfile
index c8a5068d9..807c994ce 100644
--- a/Justfile
+++ b/Justfile
@@ -1281,28 +1281,29 @@ _utils-list-feats:
exit 1
fi
- issues=$(
- gh issue list \
- --repo getarcaneapp/arcane \
- --type Feature \
- --state open \
- --limit 10000 \
- --json number,title,state,url,reactionGroups \
- --jq 'map(. + {upvotes: ([.reactionGroups[]? | select(.content == "THUMBS_UP") | .users.totalCount] | add // 0)}) | sort_by(.upvotes, .number) | reverse | .[] | [.upvotes, .number, .state, .title, .url] | @tsv'
+ discussions=$(
+ gh api graphql \
+ --paginate \
+ --slurp \
+ -f owner=getarcaneapp \
+ -f name=arcane \
+ -F endCursor=null \
+ -f query='query($owner: String!, $name: String!, $endCursor: String) { repository(owner: $owner, name: $name) { discussions(first: 100, after: $endCursor) { nodes { number title url category { slug } isAnswered upvoteCount reactionGroups { content users { totalCount } } } pageInfo { hasNextPage endCursor } } } }' \
+ | jq -r 'map(.data.repository.discussions.nodes[] | select(.category.slug == "feature-requests") | . + {upvotes: (.upvoteCount + ([.reactionGroups[]? | select(.content == "THUMBS_UP") | .users.totalCount] | add // 0))}) | sort_by(.upvotes, .number) | reverse | .[] | [.upvotes, .number, (if .isAnswered then "answered" else "open" end), .title, .url] | @tsv'
)
- if [ -z "$issues" ]; then
- echo "No open Feature issues found."
+ if [ -z "$discussions" ]; then
+ echo "No Feature discussions found."
exit 0
fi
- echo "Open Feature issues by upvotes:"
+ echo "Feature discussions by votes (upvotes + πŸ‘):"
echo ""
- while IFS=$'\t' read -r upvotes number state title url; do
- printf "%3d πŸ‘ - #%-4s [%s] %s\n" "$upvotes" "$number" "$state" "$title"
+ while IFS=$'\t' read -r votes number status title url; do
+ printf "%3d votes - #%-4s [%s] %s\n" "$votes" "$number" "$status" "$title"
printf " %s\n\n" "$url"
- done <<< "$issues"
+ done <<< "$discussions"
[group('release')]
_utils-list-fixes:
diff --git a/backend/cli/upgrade/upgrade.go b/backend/cli/upgrade/upgrade.go
index 1698f0f01..6906569f4 100644
--- a/backend/cli/upgrade/upgrade.go
+++ b/backend/cli/upgrade/upgrade.go
@@ -463,6 +463,10 @@ func UpgradeContainer(ctx context.Context, dockerClient *client.Client, oldConta
originalName := strings.TrimPrefix(oldContainer.Name, "/")
oldName := fmt.Sprintf("%s-old-%d", originalName, time.Now().UnixNano())
+ if oldContainer.Config == nil {
+ return errors.Errorf("container %s inspection has no config; refusing to recreate", originalName)
+ }
+
// Create new container config
config := *oldContainer.Config
config.Image = newImage
@@ -487,12 +491,19 @@ func UpgradeContainer(ctx context.Context, dockerClient *client.Client, oldConta
)
}
+ // Pin every attached volume to its resolved name so the recreated container
+ // keeps its data instead of receiving fresh anonymous volumes (#3953).
+ if hostConfig == nil {
+ hostConfig = &container.HostConfig{}
+ }
+ hostConfig.Binds, hostConfig.Mounts, err = docker.PreserveVolumeMounts(hostConfig.Binds, hostConfig.Mounts, oldContainer.Mounts)
+ if err != nil {
+ return errors.WrapIff(err, "preserve volumes of container %s", originalName)
+ }
+
// Fix for "conflicting options: hostname and the network mode"
// When network mode is "host" or "container:...", Hostname must be empty
- var nm container.NetworkMode
- if hostConfig != nil {
- nm = hostConfig.NetworkMode
- }
+ nm := hostConfig.NetworkMode
if nm.IsHost() || nm.IsContainer() {
config.Hostname = ""
config.Domainname = ""
@@ -509,10 +520,8 @@ 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
- if hostConfig != nil {
- hostConfig.PortBindings = nil
- hostConfig.PublishAllPorts = false
- }
+ hostConfig.PortBindings = nil
+ hostConfig.PublishAllPorts = false
}
// Build network config - preserve all network settings including IP addresses
diff --git a/backend/pkg/dockerutil/mount_utils.go b/backend/pkg/dockerutil/mount_utils.go
index 96f558d16..4144a11ac 100644
--- a/backend/pkg/dockerutil/mount_utils.go
+++ b/backend/pkg/dockerutil/mount_utils.go
@@ -2,8 +2,13 @@ package docker
import (
"context"
+ "log/slog"
+ "path"
+ "slices"
"strings"
+ "emperror.dev/errors"
+
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/mount"
"github.com/moby/moby/client"
@@ -160,3 +165,56 @@ func MountForDestination(mounts []container.MountPoint, destination string, targ
return nil
}
+
+// PreserveVolumeMounts returns cloned binds and mounts with every inspected
+// volume pinned to its name, so a recreate reuses it instead of a new anonymous one.
+func PreserveVolumeMounts(binds []string, mounts []mount.Mount, mountPoints []container.MountPoint) ([]string, []mount.Mount, error) {
+ binds = slices.Clone(binds)
+ mounts = slices.Clone(mounts)
+
+ for _, mp := range mountPoints {
+ if mp.Type != mount.TypeVolume {
+ continue
+ }
+ destination := path.Clean(mp.Destination)
+ volumeName := strings.TrimSpace(mp.Name)
+ if volumeName == "" {
+ return nil, nil, errors.Errorf("volume mounted at %s has no volume name", destination)
+ }
+
+ mountIndex := slices.IndexFunc(mounts, func(m mount.Mount) bool {
+ return path.Clean(m.Target) == destination
+ })
+ if mountIndex >= 0 {
+ if mounts[mountIndex].Type == mount.TypeVolume {
+ mounts[mountIndex].Source = volumeName
+ }
+ slog.Info("Preserving volume via mount", "destination", destination, "volume", volumeName)
+ continue
+ }
+
+ // Short syntax is "/dest" for anonymous volumes or "src:/dest[:opts]".
+ bindIndex := slices.IndexFunc(binds, func(bind string) bool {
+ parts := strings.SplitN(bind, ":", 3)
+ if len(parts) == 1 {
+ return path.Clean(parts[0]) == destination
+ }
+ return path.Clean(parts[1]) == destination
+ })
+ if bindIndex >= 0 {
+ if !strings.Contains(binds[bindIndex], ":") {
+ binds[bindIndex] = volumeName + ":" + binds[bindIndex]
+ }
+ slog.Info("Preserving volume via bind", "destination", destination, "volume", volumeName)
+ continue
+ }
+
+ explicit := MountForDestination(mountPoints, mp.Destination, mp.Destination)
+ if explicit == nil {
+ return nil, nil, errors.Errorf("cannot build explicit mount for volume %s at %s", volumeName, destination)
+ }
+ mounts = append(mounts, *explicit)
+ slog.Info("Preserving image-declared volume", "destination", destination, "volume", volumeName)
+ }
+ return binds, mounts, nil
+}
diff --git a/backend/pkg/dockerutil/mount_utils_test.go b/backend/pkg/dockerutil/mount_utils_test.go
index 6ebd015c4..86c6c16ba 100644
--- a/backend/pkg/dockerutil/mount_utils_test.go
+++ b/backend/pkg/dockerutil/mount_utils_test.go
@@ -163,3 +163,75 @@ func TestMountForSubpath(t *testing.T) {
require.Nil(t, MountForSubpath([]containertypes.MountPoint{bad}, "/app/data/projects/foo", "/workspace"))
})
}
+
+func TestPreserveVolumeMounts(t *testing.T) {
+ anon := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ mountPoints := []containertypes.MountPoint{
+ {Type: mounttypes.TypeVolume, Name: anon, Destination: "/app/data", RW: true},
+ {Type: mounttypes.TypeVolume, Name: "named", Destination: "/data", RW: true},
+ {Type: mounttypes.TypeVolume, Name: "ro", Destination: "/ro", RW: false},
+ {Type: mounttypes.TypeBind, Source: "/host", Destination: "/bind", RW: true},
+ }
+
+ tests := []struct {
+ name string
+ binds []string
+ mounts []mounttypes.Mount
+ points []containertypes.MountPoint
+ wantBinds []string
+ wantMounts []mounttypes.Mount
+ wantErr bool
+ }{
+ {
+ name: "image-declared volume gains explicit mount",
+ points: mountPoints[:1],
+ wantMounts: []mounttypes.Mount{
+ {Type: mounttypes.TypeVolume, Source: anon, Target: "/app/data"},
+ },
+ },
+ {
+ name: "anonymous bind gains volume name, explicit binds preserved",
+ binds: []string{"/app/data", "named:/data", "/host:/bind:ro"},
+ points: mountPoints,
+ wantBinds: []string{anon + ":/app/data", "named:/data", "/host:/bind:ro"},
+ wantMounts: []mounttypes.Mount{
+ {Type: mounttypes.TypeVolume, Source: "ro", Target: "/ro", ReadOnly: true},
+ },
+ },
+ {
+ name: "structured mount keeps options and gets source",
+ mounts: []mounttypes.Mount{
+ {Type: mounttypes.TypeVolume, Target: "/app/data/", VolumeOptions: &mounttypes.VolumeOptions{Subpath: "sub"}},
+ {Type: mounttypes.TypeTmpfs, Target: "/tmp"},
+ },
+ points: mountPoints[:1],
+ wantMounts: []mounttypes.Mount{
+ {Type: mounttypes.TypeVolume, Source: anon, Target: "/app/data/", VolumeOptions: &mounttypes.VolumeOptions{Subpath: "sub"}},
+ {Type: mounttypes.TypeTmpfs, Target: "/tmp"},
+ },
+ },
+ {
+ name: "volume without name fails",
+ points: []containertypes.MountPoint{{Type: mounttypes.TypeVolume, Destination: "/app/data"}},
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ origBinds := append([]string(nil), tt.binds...)
+ origMounts := append([]mounttypes.Mount(nil), tt.mounts...)
+
+ binds, mounts, err := PreserveVolumeMounts(tt.binds, tt.mounts, tt.points)
+ if tt.wantErr {
+ require.Error(t, err)
+ return
+ }
+ require.NoError(t, err)
+ require.Equal(t, tt.wantBinds, binds)
+ require.Equal(t, tt.wantMounts, mounts)
+ require.Equal(t, origBinds, tt.binds)
+ require.Equal(t, origMounts, tt.mounts)
+ })
+ }
+}