Fix multiple ports handling in the creation process and view (edit) process
- commit
- 0fbb5a45b50c18a6876b1469e0ec3b0c0cf36f94
- author
- bnema · · 2023-10-09 14:53 UTC
- parent
- b52f369
- signature
- unsigned
4 files · +22 -10
M internal/app/app.go +0 -1
| @@ -94,7 +94,6 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||
| 94 | 94 | func NewApp() *App { | |
| 95 | 95 | // Initialize AppConfig | |
| 96 | 96 | config, err := LoadConfig(AppConfig{}) | |
| 97 | - | fmt.Printf("Config: %+v\n", config) | |
| 98 | 97 | if err != nil { | |
| 99 | 98 | log.Fatalf("Failed to load configuration: %v", err) | |
| 100 | 99 | } | |
| @@ -94,7 +94,6 @@ func LoadConfig(config AppConfig) (AppConfig, error) { | |||||
| 94 | func NewApp() *App { | 94 | func NewApp() *App { | ||
| 95 | // Initialize AppConfig | 95 | // Initialize AppConfig | ||
| 96 | config, err := LoadConfig(AppConfig{}) | 96 | config, err := LoadConfig(AppConfig{}) | ||
| 97 | - | fmt.Printf("Config: %+v\n", config) | |||
| 98 | if err != nil { | 97 | if err != nil { | ||
| 99 | log.Fatalf("Failed to load configuration: %v", err) | 98 | log.Fatalf("Failed to load configuration: %v", err) | ||
| 100 | } | 99 | } | ||
M internal/httpserve/handler/createcontainer.go +2 -0
| @@ -111,6 +111,8 @@ func CreateContainerPOST(c echo.Context, a *app.App) error { | |||
| 111 | 111 | return sendError(c, err) | |
| 112 | 112 | } | |
| 113 | 113 | ||
| 114 | + | fmt.Println(cmdParams.PortMappings) | |
| 115 | + | ||
| 114 | 116 | // Create the container | |
| 115 | 117 | _, err = docker.CreateContainer(cmdParams) | |
| 116 | 118 | if err != nil { | |
| @@ -111,6 +111,8 @@ func CreateContainerPOST(c echo.Context, a *app.App) error { | |||||
| 111 | return sendError(c, err) | 111 | return sendError(c, err) | ||
| 112 | } | 112 | } | ||
| 113 | 113 | ||||
| 114 | + | fmt.Println(cmdParams.PortMappings) | |||
| 115 | + | ||||
| 114 | // Create the container | 116 | // Create the container | ||
| 115 | _, err = docker.CreateContainer(cmdParams) | 117 | _, err = docker.CreateContainer(cmdParams) | ||
| 116 | if err != nil { | 118 | if err != nil { | ||
M internal/httpserve/handler/editcontainer.go +17 -6
| @@ -51,14 +51,25 @@ func ContainerManagerEditGET(c echo.Context, a *app.App) error { | |||
| 51 | 51 | networkNames = append(networkNames, networkName) | |
| 52 | 52 | } | |
| 53 | 53 | ||
| 54 | - | // Prepare Ports while using the containerInfo.Config.ExposedPorts | |
| 54 | + | // Prepare Ports (PortBindings and ExposedPorts) | |
| 55 | 55 | portMappings := make([]string, 0) | |
| 56 | - | for exposedPort := range containerInfo.Config.ExposedPorts { | |
| 57 | - | // Here you might want to check your lastKnownPortMappings to find out what this exposedPort is mapped to on the host. | |
| 58 | - | hostPort := "" // Fetch this from lastKnownPortMappings or set a default | |
| 59 | - | portMapping := fmt.Sprintf("0.0.0.0:%s->%s", hostPort, exposedPort) | |
| 60 | - | portMappings = append(portMappings, portMapping) | |
| 56 | + | for port, bindings := range containerInfo.HostConfig.PortBindings { | |
| 57 | + | for _, binding := range bindings { | |
| 58 | + | portMappings = append(portMappings, fmt.Sprintf("%s:%s", binding.HostPort, port.Port())) | |
| 59 | + | } | |
| 61 | 60 | } | |
| 61 | + | ||
| 62 | + | exposedPorts := make([]string, 0) | |
| 63 | + | for port := range containerInfo.Config.ExposedPorts { | |
| 64 | + | exposedPorts = append(exposedPorts, port.Port()) | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | // Prepare Ports while using the containerInfo.Config.ExposedPorts | |
| 68 | + | ||
| 69 | + | for port := range containerInfo.Config.ExposedPorts { | |
| 70 | + | fmt.Println(port) | |
| 71 | + | } | |
| 72 | + | ||
| 62 | 73 | // Prepare Volumes (Mounts) | |
| 63 | 74 | volumeMappings := make([]string, 0) | |
| 64 | 75 | for _, mount := range containerInfo.Mounts { | |
| @@ -51,14 +51,25 @@ func ContainerManagerEditGET(c echo.Context, a *app.App) error { | |||||
| 51 | networkNames = append(networkNames, networkName) | 51 | networkNames = append(networkNames, networkName) | ||
| 52 | } | 52 | } | ||
| 53 | 53 | ||||
| 54 | - | // Prepare Ports while using the containerInfo.Config.ExposedPorts | 54 | + | // Prepare Ports (PortBindings and ExposedPorts) |
| 55 | portMappings := make([]string, 0) | 55 | portMappings := make([]string, 0) | ||
| 56 | - | for exposedPort := range containerInfo.Config.ExposedPorts { | 56 | + | for port, bindings := range containerInfo.HostConfig.PortBindings { |
| 57 | - | // Here you might want to check your lastKnownPortMappings to find out what this exposedPort is mapped to on the host. | 57 | + | for _, binding := range bindings { |
| 58 | - | hostPort := "" // Fetch this from lastKnownPortMappings or set a default | 58 | + | portMappings = append(portMappings, fmt.Sprintf("%s:%s", binding.HostPort, port.Port())) |
| 59 | - | portMapping := fmt.Sprintf("0.0.0.0:%s->%s", hostPort, exposedPort) | 59 | + | } |
| 60 | - | portMappings = append(portMappings, portMapping) | |||
| 61 | } | 60 | } | ||
| 61 | + | ||||
| 62 | + | exposedPorts := make([]string, 0) | |||
| 63 | + | for port := range containerInfo.Config.ExposedPorts { | |||
| 64 | + | exposedPorts = append(exposedPorts, port.Port()) | |||
| 65 | + | } | |||
| 66 | + | ||||
| 67 | + | // Prepare Ports while using the containerInfo.Config.ExposedPorts | |||
| 68 | + | ||||
| 69 | + | for port := range containerInfo.Config.ExposedPorts { | |||
| 70 | + | fmt.Println(port) | |||
| 71 | + | } | |||
| 72 | + | ||||
| 62 | // Prepare Volumes (Mounts) | 73 | // Prepare Volumes (Mounts) | ||
| 63 | volumeMappings := make([]string, 0) | 74 | volumeMappings := make([]string, 0) | ||
| 64 | for _, mount := range containerInfo.Mounts { | 75 | for _, mount := range containerInfo.Mounts { | ||
M pkg/docker/containers.go +3 -3
| @@ -200,14 +200,14 @@ func CreateContainer(cmdParams ContainerCommandParams) (string, error) { | |||
| 200 | 200 | // Prepare port bindings | |
| 201 | 201 | portBindings := nat.PortMap{} | |
| 202 | 202 | for _, portMapping := range cmdParams.PortMappings { | |
| 203 | - | key := fmt.Sprintf("%s/%s", portMapping.ExposedPort, portMapping.Protocol) | |
| 204 | - | portBindings[nat.Port(key)] = []nat.PortBinding{ | |
| 203 | + | // Prepare exposed port | |
| 204 | + | exposedPort := nat.Port(portMapping.ExposedPort + "/" + portMapping.Protocol) | |
| 205 | + | portBindings[exposedPort] = []nat.PortBinding{ | |
| 205 | 206 | { | |
| 206 | 207 | HostIP: "0.0.0.0", | |
| 207 | 208 | HostPort: portMapping.HostPort, | |
| 208 | 209 | }, | |
| 209 | 210 | } | |
| 210 | - | ||
| 211 | 211 | } | |
| 212 | 212 | ||
| 213 | 213 | // Prepare labels for Traefik | |
| @@ -200,14 +200,14 @@ func CreateContainer(cmdParams ContainerCommandParams) (string, error) { | |||||
| 200 | // Prepare port bindings | 200 | // Prepare port bindings | ||
| 201 | portBindings := nat.PortMap{} | 201 | portBindings := nat.PortMap{} | ||
| 202 | for _, portMapping := range cmdParams.PortMappings { | 202 | for _, portMapping := range cmdParams.PortMappings { | ||
| 203 | - | key := fmt.Sprintf("%s/%s", portMapping.ExposedPort, portMapping.Protocol) | 203 | + | // Prepare exposed port |
| 204 | - | portBindings[nat.Port(key)] = []nat.PortBinding{ | 204 | + | exposedPort := nat.Port(portMapping.ExposedPort + "/" + portMapping.Protocol) |
| 205 | + | portBindings[exposedPort] = []nat.PortBinding{ | |||
| 205 | { | 206 | { | ||
| 206 | HostIP: "0.0.0.0", | 207 | HostIP: "0.0.0.0", | ||
| 207 | HostPort: portMapping.HostPort, | 208 | HostPort: portMapping.HostPort, | ||
| 208 | }, | 209 | }, | ||
| 209 | } | 210 | } | ||
| 210 | - | ||||
| 211 | } | 211 | } | ||
| 212 | 212 | ||||
| 213 | // Prepare labels for Traefik | 213 | // Prepare labels for Traefik | ||