From 524e19213d5adc9a734bd150dbcbefb0294548e9 Mon Sep 17 00:00:00 2001 From: Rhevin Date: Tue, 10 Nov 2026 09:28:00 +0200 Subject: [PATCH] fix: preserve publish, mount mode, and entrypoint args container run was dropping the host IP, protocol, and read-only bit, and joining entrypoint elements into one string. --- internal/backend/apple.go | 37 ++++++++++++++--- internal/backend/apple_test.go | 75 ++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/internal/backend/apple.go b/internal/backend/apple.go index a44c127..a246f5a 100644 --- a/internal/backend/apple.go +++ b/internal/backend/apple.go @@ -159,7 +159,7 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) { } for _, p := range svc.Ports { - args = append(args, "--publish", fmt.Sprintf("%s:%d", p.Published, p.Target)) + args = append(args, "--publish", publishSpec(p)) } for k, v := range svc.Environment { @@ -176,11 +176,14 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) { } } + var entryArgs []string if svc.Entrypoint != nil { if len(svc.Entrypoint) == 0 { args = append(args, "--entrypoint", "") } else { - args = append(args, "--entrypoint", strings.Join(svc.Entrypoint, " ")) + // CLI takes one executable. Remaining entrypoint elements are args, prepended to command. + args = append(args, "--entrypoint", svc.Entrypoint[0]) + entryArgs = svc.Entrypoint[1:] } } @@ -228,14 +231,14 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) { for _, v := range svc.Volumes { switch v.Type { case "bind": - args = append(args, "--volume", fmt.Sprintf("%s:%s", v.Source, v.Target)) + args = append(args, "--volume", volumeSpec(v.Source, v.Target, v.ReadOnly)) case "volume", "": if v.Source != "" { hostPath := namedVolumePath(project, v.Source) if err := os.MkdirAll(hostPath, 0o755); err != nil { return nil, fmt.Errorf("creating volume dir %q: %w", hostPath, err) } - args = append(args, "--volume", fmt.Sprintf("%s:%s", hostPath, v.Target)) + args = append(args, "--volume", volumeSpec(hostPath, v.Target, v.ReadOnly)) } } } @@ -268,7 +271,7 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) { } args = append(args, svc.Image) - + args = append(args, entryArgs...) if len(svc.Command) > 0 { args = append(args, svc.Command...) } @@ -276,6 +279,30 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) { return args, nil } +// publishSpec is [host-ip:]host-port:container-port[/protocol]. TCP is the CLI default. +func publishSpec(p types.ServicePortConfig) string { + spec := fmt.Sprintf("%s:%d", p.Published, p.Target) + if p.HostIP != "" { + host := p.HostIP + if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") { + host = "[" + host + "]" + } + spec = host + ":" + spec + } + if p.Protocol != "" && !strings.EqualFold(p.Protocol, "tcp") { + spec += "/" + p.Protocol + } + return spec +} + +func volumeSpec(source, target string, readOnly bool) string { + spec := source + ":" + target + if readOnly { + spec += ":ro" + } + return spec +} + func namedVolumePath(project, volume string) string { home, _ := os.UserHomeDir() return filepath.Join(home, ".apple-compose", "volumes", project, volume) diff --git a/internal/backend/apple_test.go b/internal/backend/apple_test.go index dd8a6ca..5a2de43 100644 --- a/internal/backend/apple_test.go +++ b/internal/backend/apple_test.go @@ -40,6 +40,38 @@ func TestRunArgs_PortMapping(t *testing.T) { assertContains(t, args, "--publish", "8080:80") } +func TestRunArgs_PublishHostIP(t *testing.T) { + svc := types.ServiceConfig{ + Name: "web", + Image: "nginx:alpine", + Ports: []types.ServicePortConfig{ + {HostIP: "127.0.0.1", Published: "8080", Target: 80}, + {HostIP: "::1", Published: "8080", Target: 80}, + }, + } + args, err := RunArgs("myapp", svc) + if err != nil { + t.Fatal(err) + } + assertContains(t, args, "--publish", "127.0.0.1:8080:80") + assertContains(t, args, "--publish", "[::1]:8080:80") +} + +func TestRunArgs_PublishProtocol(t *testing.T) { + svc := types.ServiceConfig{ + Name: "dns", + Image: "nginx:alpine", + Ports: []types.ServicePortConfig{ + {Published: "5353", Target: 53, Protocol: "udp"}, + }, + } + args, err := RunArgs("myapp", svc) + if err != nil { + t.Fatal(err) + } + assertContains(t, args, "--publish", "5353:53/udp") +} + func TestRunArgs_EnvVars(t *testing.T) { svc := types.ServiceConfig{ Name: "db", @@ -96,6 +128,21 @@ func TestRunArgs_BindMount(t *testing.T) { assertContains(t, args, "--volume", "/host/data:/var/lib/postgresql/data") } +func TestRunArgs_BindMountReadOnly(t *testing.T) { + svc := types.ServiceConfig{ + Name: "db", + Image: "postgres:16", + Volumes: []types.ServiceVolumeConfig{ + {Type: "bind", Source: "/host/data", Target: "/var/lib/postgresql/data", ReadOnly: true}, + }, + } + args, err := RunArgs("myapp", svc) + if err != nil { + t.Fatal(err) + } + assertContains(t, args, "--volume", "/host/data:/var/lib/postgresql/data:ro") +} + func TestRunArgs_Entrypoint(t *testing.T) { svc := types.ServiceConfig{ Name: "web", @@ -109,6 +156,25 @@ func TestRunArgs_Entrypoint(t *testing.T) { assertContains(t, args, "--entrypoint", "/docker-entrypoint.sh") } +func TestRunArgs_EntrypointArgsPrependedToCommand(t *testing.T) { + svc := types.ServiceConfig{ + Name: "web", + Image: "nginx:alpine", + Entrypoint: types.ShellCommand{"sh", "-c", "echo hi"}, + Command: types.ShellCommand{"nginx"}, + } + args, err := RunArgs("myapp", svc) + if err != nil { + t.Fatal(err) + } + assertContains(t, args, "--entrypoint", "sh") + got := argsAfter(args, "nginx:alpine") + want := []string{"-c", "echo hi", "nginx"} + if strings.Join(got, "\x00") != strings.Join(want, "\x00") { + t.Fatalf("args after image: got %q want %q", got, want) + } +} + func TestRunArgs_UserAndWorkdir(t *testing.T) { svc := types.ServiceConfig{ Name: "web", @@ -423,6 +489,15 @@ func TestAppleContainer_UnmarshalContainerCLI141JSON(t *testing.T) { } } +func argsAfter(args []string, token string) []string { + for i, a := range args { + if a == token { + return args[i+1:] + } + } + return nil +} + // assertContains checks that key and value appear consecutively in args. func assertContains(t *testing.T, args []string, key, value string) { t.Helper()