From 3ca1aa9cb1ce4b02738028712a7dcf7d2129c7bc Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 12 Mar 2025 03:16:08 -0400 Subject: [PATCH] engine: resources: Fix backwards docker ports This wasn't setup properly, now it's fixed. Woops. --- docs/resources.md | 2 +- engine/resources/docker_container.go | 30 ++++++++++++++++++++-------- examples/lang/docker0.mcl | 2 +- examples/lang/docker_container0.mcl | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/docs/resources.md b/docs/resources.md index 6c2bdf11..fefaa923 100644 --- a/docs/resources.md +++ b/docs/resources.md @@ -56,7 +56,7 @@ It has the following properties: * `image`: docker `image` or `image:tag` * `cmd`: a command or list of commands to run on the container * `env`: a list of environment variables, e.g. `["VAR=val",],` -* `ports`: a map of portmappings, e.g. `{"tcp" => {80 => 8080, 443 => 8443,},},` +* `ports`: a map of portmappings, e.g. `{"tcp" => {8080 => 80, 8443 => 443,},},` * `apiversion:` override the host's default docker version, e.g. `"v1.35"` * `force`: destroy and rebuild the container instead of erroring on wrong image diff --git a/engine/resources/docker_container.go b/engine/resources/docker_container.go index 0e3d0299..385a6cb6 100644 --- a/engine/resources/docker_container.go +++ b/engine/resources/docker_container.go @@ -81,7 +81,9 @@ type DockerContainerRes struct { // Env is a list of environment variables. E.g. ["VAR=val",]. Env []string `lang:"env" yaml:"env"` - // Ports is a map of port bindings. E.g. {"tcp" => {80 => 8080},}. + // Ports is a map of port bindings. E.g. {"tcp" => {8080 => 80},}. The + // key is the host port, and the val is the inner service port to + // forward to. Ports map[string]map[int64]int64 `lang:"ports" yaml:"ports"` // APIVersion allows you to override the host's default client API @@ -261,6 +263,8 @@ func (obj *DockerContainerRes) CheckApply(ctx context.Context, apply bool) (bool } } + // XXX: Check if defined ports matches what we expect. + if !apply { return false, nil } @@ -309,15 +313,25 @@ func (obj *DockerContainerRes) CheckApply(ctx context.Context, apply bool) (bool PortBindings: make(map[nat.Port][]nat.PortBinding), } - for k, v := range obj.Ports { + for proto, v := range obj.Ports { + // On the outside, on the host, we'd see 8080 which is p + // and on the inside, the container would have something + // running on 80, which is q. for p, q := range v { - containerConfig.ExposedPorts[nat.Port(k)] = struct{}{} - hostConfig.PortBindings[nat.Port(fmt.Sprintf("%d/%s", p, k))] = []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: fmt.Sprintf("%d", q), - }, + // Port is a string containing port number and + // protocol in the format "80/tcp". + port := fmt.Sprintf("%d/%s", q, proto) + n := nat.Port(port) + containerConfig.ExposedPorts[n] = struct{}{} // PortSet + + pb := nat.PortBinding{ + HostIP: "0.0.0.0", + HostPort: fmt.Sprintf("%d", p), // eg: 8080 } + if _, exists := hostConfig.PortBindings[n]; !exists { + hostConfig.PortBindings[n] = []nat.PortBinding{} + } + hostConfig.PortBindings[n] = append(hostConfig.PortBindings[n], pb) } } diff --git a/examples/lang/docker0.mcl b/examples/lang/docker0.mcl index e6bf7ae7..a9264906 100644 --- a/examples/lang/docker0.mcl +++ b/examples/lang/docker0.mcl @@ -2,7 +2,7 @@ docker:container "mgmt-nginx" { state => "running", image => "nginx", cmd => ["nginx", "-g", "daemon off;",], - ports => {"tcp" => {80 => 8080,},}, + ports => {"tcp" => {8080 => 80,},}, } docker:image "nginx" { diff --git a/examples/lang/docker_container0.mcl b/examples/lang/docker_container0.mcl index 93eed38f..b8ec993b 100644 --- a/examples/lang/docker_container0.mcl +++ b/examples/lang/docker_container0.mcl @@ -2,5 +2,5 @@ docker:container "mgmt-nginx" { state => "running", image => "nginx", cmd => ["nginx", "-g", "daemon off;",], - ports => {"tcp" => {80 => 8080,},}, + ports => {"tcp" => {8080 => 80,},}, }