Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: container mount inspection logic
Kyle Mendell0bbf8dc99210
diff --git a/backend/pkg/dockerutil/mount_utils.go b/backend/pkg/dockerutil/mount_utils.goindex 2605c770c..376c2329f 100644--- a/backend/pkg/dockerutil/mount_utils.go+++ b/backend/pkg/dockerutil/mount_utils.go@@ -20,14 +20,14 @@ func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client,return "", nil // No docker client, can't discover}- // 1. Get current container ID (usually the short ID is the hostname)- hostname, err := os.Hostname()+ // 1. Prefer robust current-container detection and fall back to hostname.+ inspectTarget, err := getCurrentContainerInspectTargetInternal(GetCurrentContainerID, os.Hostname)if err != nil {return "", err}// 2. Inspect self- inspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, hostname, client.ContainerInspectOptions{})+ inspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, inspectTarget, client.ContainerInspectOptions{})if err != nil {// Not running in a container or can't reach docker daemonreturn "", err@@ -70,6 +70,27 @@ func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client,return "", nil}+func getCurrentContainerInspectTargetInternal(currentContainerID func() (string, error), hostname func() (string, error)) (string, error) {+ if currentContainerID != nil {+ if containerID, err := currentContainerID(); err == nil {+ if containerID = strings.TrimSpace(containerID); containerID != "" {+ return containerID, nil+ }+ }+ }++ if hostname == nil {+ hostname = os.Hostname+ }++ value, err := hostname()+ if err != nil {+ return "", err+ }++ return strings.TrimSpace(value), nil+}+// MountForDestination returns a Mount suitable for container creation that mirrors an// existing container mount at the given destination.//diff --git a/backend/pkg/dockerutil/mount_utils_test.go b/backend/pkg/dockerutil/mount_utils_test.goindex 2d0cff698..a4eb6a472 100644--- a/backend/pkg/dockerutil/mount_utils_test.go+++ b/backend/pkg/dockerutil/mount_utils_test.go@@ -1,6 +1,7 @@package dockerimport (+ "errors""testing"containertypes "github.com/moby/moby/api/types/container"@@ -8,6 +9,49 @@ import ("github.com/stretchr/testify/require")+func TestGetCurrentContainerInspectTargetInternal(t *testing.T) {+ t.Run("prefers detected container id over hostname", func(t *testing.T) {+ target, err := getCurrentContainerInspectTargetInternal(+ func() (string, error) { return "0123456789ab", nil },+ func() (string, error) { return "rpi4", nil },+ )++ require.NoError(t, err)+ require.Equal(t, "0123456789ab", target)+ })++ t.Run("falls back to hostname when container id unavailable", func(t *testing.T) {+ target, err := getCurrentContainerInspectTargetInternal(+ func() (string, error) { return "", errors.New("no container id found") },+ func() (string, error) { return "rpi4", nil },+ )++ require.NoError(t, err)+ require.Equal(t, "rpi4", target)+ })++ t.Run("trims whitespace from detected container id", func(t *testing.T) {+ target, err := getCurrentContainerInspectTargetInternal(+ func() (string, error) { return " 0123456789ab ", nil },+ func() (string, error) { return "rpi4", nil },+ )++ require.NoError(t, err)+ require.Equal(t, "0123456789ab", target)+ })++ t.Run("returns hostname error when fallback fails", func(t *testing.T) {+ target, err := getCurrentContainerInspectTargetInternal(+ func() (string, error) { return "", errors.New("no container id found") },+ func() (string, error) { return "", errors.New("hostname unavailable") },+ )++ require.Error(t, err)+ require.Contains(t, err.Error(), "hostname unavailable")+ require.Equal(t, "", target)+ })+}+func TestMountForDestination(t *testing.T) {tests := []struct {name string