Skip to content

Commit 25d6ba5

Browse files
lucaslorentzclaude
andcommitted
Strip leading slash from container names in template data
The Docker API returns container names prefixed with "/" (e.g. "/mycontainer"), so {{index .Names 0}} produced "/mycontainer" instead of "mycontainer" as documented in the README. Fixes #648 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 89dc140 commit 25d6ba5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

generator/containers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package generator
22

33
import (
4+
"strings"
5+
46
"github.com/docker/docker/api/types"
57
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
68
"go.uber.org/zap"
@@ -9,11 +11,21 @@ import (
911
func (g *CaddyfileGenerator) getContainerCaddyfile(container *types.Container, logger *zap.Logger) (*caddyfile.Container, error) {
1012
caddyLabels := g.filterLabels(container.Labels)
1113

14+
trimContainerNames(container)
15+
1216
return labelsToCaddyfile(caddyLabels, container, func() ([]string, error) {
1317
return g.getContainerIPAddresses(container, logger, true)
1418
})
1519
}
1620

21+
// trimContainerNames removes the leading slash from container names.
22+
// The Docker API returns names prefixed with "/" (e.g. "/mycontainer").
23+
func trimContainerNames(container *types.Container) {
24+
for i, name := range container.Names {
25+
container.Names[i] = strings.TrimPrefix(name, "/")
26+
}
27+
}
28+
1729
func (g *CaddyfileGenerator) getContainerIPAddresses(container *types.Container, logger *zap.Logger, onlyIngressIps bool) ([]string, error) {
1830
ips := []string{}
1931

generator/containers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestContainers_TemplateData(t *testing.T) {
1313
dockerClient.ContainersData = []types.Container{
1414
{
1515
Names: []string{
16-
"container-name",
16+
"/container-name",
1717
},
1818
NetworkSettings: &types.SummaryNetworkSettings{
1919
Networks: map[string]*network.EndpointSettings{

0 commit comments

Comments
 (0)