Skip to content

Commit 18d2b9e

Browse files
committed
Fix publish short-form port mappings
Signed-off-by: CodeLoopdroid <214800619+CodeLoopdroid@users.noreply.github.com>
1 parent ef86a6e commit 18d2b9e

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

pkg/compose/publish.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/opencontainers/image-spec/specs-go"
3737
v1 "github.com/opencontainers/image-spec/specs-go/v1"
3838
"github.com/sirupsen/logrus"
39+
"go.yaml.in/yaml/v4"
3940

4041
"github.com/docker/compose/v5/internal/oci"
4142
"github.com/docker/compose/v5/pkg/api"
@@ -476,7 +477,7 @@ func composeFileAsByteReader(ctx context.Context, filePath string, project *type
476477
if err != nil {
477478
return nil, fmt.Errorf("failed to open compose file %s: %w", filePath, err)
478479
}
479-
base, err := loader.LoadWithContext(ctx, types.ConfigDetails{
480+
model, err := loader.LoadModelWithContext(ctx, types.ConfigDetails{
480481
WorkingDir: project.WorkingDir,
481482
Environment: project.Environment,
482483
ConfigFiles: []types.ConfigFile{
@@ -497,7 +498,7 @@ func composeFileAsByteReader(ctx context.Context, filePath string, project *type
497498
return nil, err
498499
}
499500

500-
in, err := base.MarshalYAML()
501+
in, err := yaml.Marshal(model)
501502
if err != nil {
502503
return nil, err
503504
}

pkg/compose/publish_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package compose
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"slices"
2123
"testing"
2224

@@ -100,3 +102,31 @@ services:
100102
return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
101103
}, cmp.Ignore()))
102104
}
105+
106+
func TestComposeFileAsByteReaderWithShortFormPortsAndInterpolation(t *testing.T) {
107+
dir := t.TempDir()
108+
composePath := filepath.Join(dir, "compose.yaml")
109+
err := os.WriteFile(composePath, []byte(`name: publish-test
110+
services:
111+
whoami:
112+
image: docker.io/traefik/whoami:v1.11
113+
ports:
114+
- ${DASHBOARD_PORT:-3000}:3000
115+
`), 0o600)
116+
assert.NilError(t, err)
117+
118+
project, err := loader.LoadWithContext(t.Context(), types.ConfigDetails{
119+
WorkingDir: dir,
120+
Environment: types.Mapping{
121+
"DASHBOARD_PORT": "",
122+
},
123+
ConfigFiles: []types.ConfigFile{
124+
{Filename: composePath},
125+
},
126+
})
127+
assert.NilError(t, err)
128+
129+
reader, err := composeFileAsByteReader(t.Context(), composePath, project)
130+
assert.NilError(t, err)
131+
assert.Assert(t, reader != nil)
132+
}

pkg/e2e/publish_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package e2e
1818

1919
import (
2020
"fmt"
21+
"os"
22+
"path/filepath"
2123
"strings"
2224
"testing"
2325

@@ -60,6 +62,23 @@ or remove sensitive data from your Compose configuration
6062
assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
6163
})
6264

65+
t.Run("publish success short-form port mapping", func(t *testing.T) {
66+
dir := t.TempDir()
67+
composePath := filepath.Join(dir, "compose-short-port.yml")
68+
err := os.WriteFile(composePath, []byte(`services:
69+
whoami:
70+
image: docker.io/traefik/whoami:v1.11
71+
ports:
72+
- ${DASHBOARD_PORT:-3000}:3000
73+
`), 0o600)
74+
assert.NilError(t, err)
75+
76+
res := c.RunDockerComposeCmd(t, "-f", composePath,
77+
"-p", projectName, "publish", "test/test", "--with-env", "-y", "--dry-run")
78+
assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
79+
assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
80+
})
81+
6382
t.Run("publish with extends", func(t *testing.T) {
6483
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-with-extends.yml",
6584
"-p", projectName, "publish", "test/test", "--dry-run")

0 commit comments

Comments
 (0)