engine: resources: Fix backwards docker ports

This wasn't setup properly, now it's fixed. Woops.
This commit is contained in:
James Shubin
2025-03-12 03:16:08 -04:00
parent 37308b950b
commit 3ca1aa9cb1
4 changed files with 25 additions and 11 deletions

View File

@@ -56,7 +56,7 @@ It has the following properties:
* `image`: docker `image` or `image:tag` * `image`: docker `image` or `image:tag`
* `cmd`: a command or list of commands to run on the container * `cmd`: a command or list of commands to run on the container
* `env`: a list of environment variables, e.g. `["VAR=val",],` * `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"` * `apiversion:` override the host's default docker version, e.g. `"v1.35"`
* `force`: destroy and rebuild the container instead of erroring on wrong image * `force`: destroy and rebuild the container instead of erroring on wrong image

View File

@@ -81,7 +81,9 @@ type DockerContainerRes struct {
// Env is a list of environment variables. E.g. ["VAR=val",]. // Env is a list of environment variables. E.g. ["VAR=val",].
Env []string `lang:"env" yaml:"env"` 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"` Ports map[string]map[int64]int64 `lang:"ports" yaml:"ports"`
// APIVersion allows you to override the host's default client API // 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 { if !apply {
return false, nil return false, nil
} }
@@ -309,15 +313,25 @@ func (obj *DockerContainerRes) CheckApply(ctx context.Context, apply bool) (bool
PortBindings: make(map[nat.Port][]nat.PortBinding), 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 { for p, q := range v {
containerConfig.ExposedPorts[nat.Port(k)] = struct{}{} // Port is a string containing port number and
hostConfig.PortBindings[nat.Port(fmt.Sprintf("%d/%s", p, k))] = []nat.PortBinding{ // 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", HostIP: "0.0.0.0",
HostPort: fmt.Sprintf("%d", q), 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)
} }
} }

View File

@@ -2,7 +2,7 @@ docker:container "mgmt-nginx" {
state => "running", state => "running",
image => "nginx", image => "nginx",
cmd => ["nginx", "-g", "daemon off;",], cmd => ["nginx", "-g", "daemon off;",],
ports => {"tcp" => {80 => 8080,},}, ports => {"tcp" => {8080 => 80,},},
} }
docker:image "nginx" { docker:image "nginx" {

View File

@@ -2,5 +2,5 @@ docker:container "mgmt-nginx" {
state => "running", state => "running",
image => "nginx", image => "nginx",
cmd => ["nginx", "-g", "daemon off;",], cmd => ["nginx", "-g", "daemon off;",],
ports => {"tcp" => {80 => 8080,},}, ports => {"tcp" => {8080 => 80,},},
} }