Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: fix dind path mappings for projects and swarm (#2939)
Kyle Mendell480d69365ad7
diff --git a/backend/internal/services/gitops_sync_service.go b/backend/internal/services/gitops_sync_service.goindex 1c6d5327b..a805124ad 100644--- a/backend/internal/services/gitops_sync_service.go+++ b/backend/internal/services/gitops_sync_service.go@@ -1656,7 +1656,7 @@ func (s *GitOpsSyncService) validateDirectorySyncStageInternal(ctx context.Contereturn 0, err}- pathMapper := s.projectService.getPathMapper(ctx)+ pathMapper := s.projectService.getPathMapperInternal(ctx)autoInjectEnv := s.settingsService.GetBoolSetting(ctx, "autoInjectEnv", false)project, err := projects.LoadComposeProjectLenient(diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.goindex 03441603c..07e6e7049 100644--- a/backend/internal/services/project_service.go+++ b/backend/internal/services/project_service.go@@ -99,7 +99,7 @@ func (s *ProjectService) resolveRegistryCredentialsInternal(ctx context.Context)return credentials, nil}-func (s *ProjectService) getPathMapper(ctx context.Context) *projects.PathMapper {+func (s *ProjectService) getPathMapperInternal(ctx context.Context) *projects.PathMapper {configuredPath := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "/app/data/projects")var containerDir, hostDir string@@ -124,29 +124,32 @@ func (s *ProjectService) getPathMapper(ctx context.Context) *projects.PathMappercontainerDirResolved = "/app/data/projects"}- // If hostDir not obtained from mapping, attempt auto-discovery from Docker mounts- if hostDir == "" && s.dockerService != nil {- if dockerCli, derr := s.dockerService.GetClient(ctx); derr == nil {- absContainerDir, _ := filepath.Abs(containerDirResolved)- if discovery, aerr := dockerutil.GetHostPathForContainerPath(ctx, dockerCli, absContainerDir); aerr == nil && discovery != "" {- hostDir = discovery- slog.DebugContext(ctx, "Auto-discovered host path for projects", "container", absContainerDir, "host", hostDir)- }+ // Explicit "container:host" mapping: honor the user-declared prefix directly.+ if strings.TrimSpace(hostDir) != "" {+ hostDirResolved := filepath.Clean(strings.TrimSpace(hostDir))+ pm := projects.NewPathMapper(containerDirResolved, hostDirResolved)+ if !pm.IsNonMatchingMount() {+ return nil}+ return pm}- // Clean host directory- hostDirResolved := strings.TrimSpace(hostDir)- if hostDirResolved != "" {- hostDirResolved = filepath.Clean(hostDirResolved)- }-- pm := projects.NewPathMapper(containerDirResolved, hostDirResolved)- if !pm.IsNonMatchingMount() {- return nil+ // Auto-discovery: resolve each bind-mount source against Arcane's real container+ // mount table (longest-prefix match) so independently bind-mounted project+ // directories map to their own host path instead of a single projects-root prefix.+ if s.dockerService != nil {+ if dockerCli, derr := s.dockerService.GetClient(ctx); derr == nil {+ if mounts, merr := dockerutil.GetCurrentContainerMounts(ctx, dockerCli); merr == nil && len(mounts) > 0 {+ pm := projects.NewPathMapperFromMounts(mounts)+ if !pm.IsNonMatchingMount() {+ return nil+ }+ return pm+ }+ }}- return pm+ return nil}func (s *ProjectService) getProjectsDirectoryInternal(ctx context.Context) (string, error) {@@ -501,7 +504,7 @@ func (s *ProjectService) loadComposeProjectForProjectInternal(ctx context.ContexprojectsDirectory = "/app/data/projects"}- pathMapper := s.getPathMapper(ctx)+ pathMapper := s.getPathMapperInternal(ctx)composeProject, loadErr := projects.LoadComposeProject(ctx, composeFileFullPath, normalizeComposeProjectName(proj.Name), projectsDirectory, utils.BoolOrDefault(cfg.AutoInjectEnv.Value, false), pathMapper)if loadErr != nil {@@ -2760,7 +2763,7 @@ func (s *ProjectService) RestartProject(ctx context.Context, projectID string, uprojectsDirectory = "/app/data/projects"}- pathMapper := s.getPathMapper(ctx)+ pathMapper := s.getPathMapperInternal(ctx)compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, normalizeComposeProjectName(proj.Name), projectsDirectory, utils.BoolOrDefault(cfg.AutoInjectEnv.Value, false), pathMapper)if lerr != nil {@@ -4478,7 +4481,7 @@ func (s *ProjectService) loadComposeMetadataForSyncInternal(ctx context.Context,return 0, nil, pErr}- pathMapper := s.getPathMapper(ctx)+ pathMapper := s.getPathMapperInternal(ctx)normName := normalizeComposeProjectName(dirName)autoInjectEnv := utils.BoolOrDefault(cfg.AutoInjectEnv.Value, false)diff --git a/backend/internal/services/swarm_service.go b/backend/internal/services/swarm_service.goindex 855d4040f..ca20b6451 100644--- a/backend/internal/services/swarm_service.go+++ b/backend/internal/services/swarm_service.go@@ -2259,29 +2259,32 @@ func (s *SwarmService) getPathMapperInternal(ctx context.Context) *appfs.PathMapcontainerDirResolved = defaultSwarmStackSourceRootDir}- // If hostDir not obtained from mapping, attempt auto-discovery from Docker mounts- if hostDir == "" && s.dockerService != nil {- if dockerCli, derr := s.dockerService.GetClient(ctx); derr == nil {- absContainerDir, _ := filepath.Abs(containerDirResolved)- if discovery, aerr := dockerutil.GetHostPathForContainerPath(ctx, dockerCli, absContainerDir); aerr == nil && discovery != "" {- hostDir = discovery- slog.DebugContext(ctx, "Auto-discovered host path for swarm stack sources", "container", absContainerDir, "host", hostDir)- }+ // Explicit "container:host" mapping: honor the user-declared prefix directly.+ if strings.TrimSpace(hostDir) != "" {+ hostDirResolved := filepath.Clean(strings.TrimSpace(hostDir))+ pm := appfs.NewPathMapper(containerDirResolved, hostDirResolved)+ if !pm.IsNonMatchingMount() {+ return nil}+ return pm}- // Clean host directory- hostDirResolved := strings.TrimSpace(hostDir)- if hostDirResolved != "" {- hostDirResolved = filepath.Clean(hostDirResolved)- }-- pm := appfs.NewPathMapper(containerDirResolved, hostDirResolved)- if !pm.IsNonMatchingMount() {- return nil+ // Auto-discovery: resolve each bind-mount source against Arcane's real container+ // mount table (longest-prefix match) so an independently bind-mounted stack+ // directory maps to its own host path instead of a single sources-root prefix.+ if s.dockerService != nil {+ if dockerCli, derr := s.dockerService.GetClient(ctx); derr == nil {+ if mounts, merr := dockerutil.GetCurrentContainerMounts(ctx, dockerCli); merr == nil && len(mounts) > 0 {+ pm := appfs.NewPathMapperFromMounts(mounts)+ if !pm.IsNonMatchingMount() {+ return nil+ }+ return pm+ }+ }}- return pm+ return nil}func (s *SwarmService) ensureSwarmManagerInternal(ctx context.Context) error {diff --git a/backend/pkg/dockerutil/mount_utils.go b/backend/pkg/dockerutil/mount_utils.goindex c3c1459c2..e080f601a 100644--- a/backend/pkg/dockerutil/mount_utils.go+++ b/backend/pkg/dockerutil/mount_utils.go@@ -12,59 +12,53 @@ import ("github.com/moby/moby/client")-// GetHostPathForContainerPath attempts to discover the host-side path for a given container path-// by inspecting the container itself. This is useful for Docker-in-Docker scenarios-// where the application needs to know host paths for volume mapping.-func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client, containerPath string) (string, error) {+// GetCurrentContainerMounts inspects Arcane's own container and returns its bind and+// named-volume mounts as projects.HostMount entries. It returns no mounts when Arcane is+// not running in a container (or the daemon is unreachable). This is the basis for+// Docker-in-Docker host-path resolution.+func GetCurrentContainerMounts(ctx context.Context, dockerCli *client.Client) ([]projects.HostMount, error) {if dockerCli == nil {- return "", nil // No docker client, can't discover+ return nil, nil // No docker client, can't discover}- // 1. Prefer robust current-container detection and fall back to hostname.+ // Prefer robust current-container detection and fall back to hostname.inspectTarget, err := getCurrentContainerInspectTargetInternal(GetCurrentContainerID, os.Hostname)if err != nil {- return "", err+ return nil, err}- // 2. Inspect selfinspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, inspectTarget, client.ContainerInspectOptions{})if err != nil {// Not running in a container or can't reach docker daemon- return "", err+ return nil, err}- // 3. Find mount point for the target path- // We want to find the mount that most specifically matches our path- var bestMatch *containertypes.MountPoint+ mounts := make([]projects.HostMount, 0, len(inspect.Container.Mounts))for i := range inspect.Container.Mounts {m := &inspect.Container.Mounts[i]- if strings.HasPrefix(containerPath, m.Destination) {- if bestMatch == nil || len(m.Destination) > len(bestMatch.Destination) {- bestMatch = m- }+ if m.Type != mounttypes.TypeBind && m.Type != mounttypes.TypeVolume {+ continue}+ if strings.TrimSpace(m.Source) == "" || strings.TrimSpace(m.Destination) == "" {+ continue+ }+ mounts = append(mounts, projects.HostMount{Destination: m.Destination, Source: m.Source})}+ return mounts, nil+}- if bestMatch != nil && (bestMatch.Type == mounttypes.TypeBind || bestMatch.Type == mounttypes.TypeVolume) {- // Calculate the relative path from mount destination to target path- rel := strings.TrimPrefix(containerPath, bestMatch.Destination)- rel = strings.TrimPrefix(rel, "/") // Ensure no double slash-- hostPath := bestMatch.Source- if rel != "" {- // Determine path separator from the host path- separator := "/"- if projects.IsWindowsDrivePath(hostPath) && strings.Contains(hostPath, "\\") {- separator = "\\"- rel = strings.ReplaceAll(rel, "/", "\\")- }+// GetHostPathForContainerPath attempts to discover the host-side path for a given container path+// by inspecting the container itself. This is useful for Docker-in-Docker scenarios+// where the application needs to know host paths for volume mapping. It returns an empty+// string when the path is not covered by any of Arcane's mounts.+func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client, containerPath string) (string, error) {+ mounts, err := GetCurrentContainerMounts(ctx, dockerCli)+ if err != nil {+ return "", err+ }- if !strings.HasSuffix(hostPath, separator) {- hostPath += separator- }- hostPath += rel- }- return hostPath, nil+ if host, ok := projects.ResolveHostPath(mounts, containerPath); ok {+ return host, nil}return "", nildiff --git a/backend/pkg/projects/path_mapper.go b/backend/pkg/projects/path_mapper.goindex 4a6965dba..7ebc4b5b8 100644--- a/backend/pkg/projects/path_mapper.go+++ b/backend/pkg/projects/path_mapper.go@@ -8,11 +8,21 @@ import (composetypes "github.com/compose-spec/compose-go/v2/types")+// HostMount is one container→host mount (bind or named volume) from Arcane's own+// container. It is used for longest-prefix host-path resolution in Docker-in-Docker+// setups, where independently bind-mounted project directories each map to their own+// host path rather than a single projects-root prefix.+type HostMount struct {+ Destination string // container-side mount path, e.g. "/app/data/projects/homeassistant"+ Source string // host-side path, e.g. "/home/user/homeassistant"+}+// PathMapper handles translation between container and host pathstype PathMapper struct {- containerPrefix string // e.g., "/app/data/projects"- hostPrefix string // e.g., "D:/self-hosted/arcane/projects"- isNonMatching bool // true if paths differ+ containerPrefix string // e.g., "/app/data/projects" (single-prefix mode)+ hostPrefix string // e.g., "D:/self-hosted/arcane/projects" (single-prefix mode)+ isNonMatching bool // true if a translation can occur+ mounts []HostMount // when set, sources resolve by longest-prefix match (auto-discovery mode)}// NewPathMapper creates a new path mapper@@ -31,12 +41,84 @@ func NewPathMapper(containerDir, hostDir string) *PathMapper {}}+// NewPathMapperFromMounts creates a path mapper that resolves each source against the+// given container mount table by longest-prefix match, instead of a single+// container→host prefix. This is used for Docker-in-Docker auto-discovery so that an+// independently bind-mounted project directory maps to its real host path.+func NewPathMapperFromMounts(mounts []HostMount) *PathMapper {+ nonMatching := false+ for i := range mounts {+ if filepath.Clean(mounts[i].Source) != filepath.Clean(mounts[i].Destination) {+ nonMatching = true+ break+ }+ }++ return &PathMapper{+ mounts: mounts,+ isNonMatching: nonMatching,+ }+}++// ResolveHostPath returns the host-side path for containerPath by selecting the+// longest-prefix mount whose Destination contains it and appending the trailing relative+// segment to that mount's Source. ok is false when no mount contains the path.+func ResolveHostPath(mounts []HostMount, containerPath string) (string, bool) {+ cleaned := filepath.Clean(containerPath)++ var (+ bestSource string+ bestRel string+ bestLen = -1+ )+ for i := range mounts {+ dest := filepath.Clean(mounts[i].Destination)+ rel, err := filepath.Rel(dest, cleaned)+ if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || filepath.IsAbs(rel) {+ continue+ }+ if len(dest) > bestLen {+ bestLen = len(dest)+ bestSource = mounts[i].Source+ bestRel = rel+ }+ }+ if bestLen < 0 {+ return "", false+ }++ host := bestSource+ if bestRel != "." {+ // Mirror the separator handling used for Windows hosts: keep backslashes when the+ // host source is a Windows drive path, otherwise use forward slashes.+ separator := "/"+ if IsWindowsDrivePath(host) && strings.Contains(host, "\\") {+ separator = "\\"+ bestRel = strings.ReplaceAll(bestRel, "/", "\\")+ }+ if !strings.HasSuffix(host, separator) {+ host += separator+ }+ host += bestRel+ }+ return host, true+}+// ContainerToHost translates a container path to host pathfunc (pm *PathMapper) ContainerToHost(containerPath string) (string, error) {if !pm.IsNonMatchingMount() {return containerPath, nil // No translation needed}+ // Auto-discovery mode: resolve against Arcane's real mount table so nested,+ // independently bind-mounted directories map to their own host path.+ if len(pm.mounts) > 0 {+ if host, ok := ResolveHostPath(pm.mounts, containerPath); ok {+ return host, nil+ }+ return filepath.Clean(containerPath), nil // outside all mounts: leave unchanged+ }+cleaned := filepath.Clean(containerPath)// Calculate relative pathdiff --git a/backend/pkg/projects/path_mapper_test.go b/backend/pkg/projects/path_mapper_test.goindex 55a19ede9..b826d6df8 100644--- a/backend/pkg/projects/path_mapper_test.go+++ b/backend/pkg/projects/path_mapper_test.go@@ -36,6 +36,43 @@ func TestPathMapper_PathTraversalPrevention(t *testing.T) {assert.Equal(t, "/app/etc/passwd", result)}+func TestPathMapper_FromMounts_NestedIndependentMount(t *testing.T) {+ // Reproduces the nested independent-mount bug: a project directory is bind-mounted+ // into Arcane's projects directory from an unrelated host path.+ pm := NewPathMapperFromMounts([]HostMount{+ {Destination: "/app/data", Source: "/home/user/.arcane/data"},+ {Destination: "/app/data/projects/homeassistant", Source: "/home/user/homeassistant"},+ })+ require.True(t, pm.IsNonMatchingMount())++ // Independently-mounted project resolves to its own host path (longest-prefix wins).+ got, err := pm.ContainerToHost("/app/data/projects/homeassistant/service_postgresql/postgres_data")+ require.NoError(t, err)+ assert.Equal(t, "/home/user/homeassistant/service_postgresql/postgres_data", got)++ // A project without its own mount still re-bases under /app/data.+ got, err = pm.ContainerToHost("/app/data/projects/other/data")+ require.NoError(t, err)+ assert.Equal(t, "/home/user/.arcane/data/projects/other/data", got)+}++func TestPathMapper_FromMounts_NoMatchAndMatchingMounts(t *testing.T) {+ pm := NewPathMapperFromMounts([]HostMount{+ {Destination: "/app/data", Source: "/host/data"},+ })++ // Path outside every mount is returned unchanged.+ got, err := pm.ContainerToHost("/etc/hosts")+ require.NoError(t, err)+ assert.Equal(t, "/etc/hosts", got)++ // A table whose mounts all match (source == destination) needs no translation.+ matching := NewPathMapperFromMounts([]HostMount{+ {Destination: "/var/run/docker.sock", Source: "/var/run/docker.sock"},+ })+ assert.False(t, matching.IsNonMatchingMount())+}+func TestPathMapper_TranslateVolumeSources(t *testing.T) {pm := NewPathMapper("/app/data/projects", "C:/User/arcane/projects")