detached 0fbb5a4

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) {
9494 func NewApp() *App {
9595 // Initialize AppConfig
9696 config, err := LoadConfig(AppConfig{})
97- fmt.Printf("Config: %+v\n", config)
9897 if err != nil {
9998 log.Fatalf("Failed to load configuration: %v", err)
10099 }
@@ -94,7 +94,6 @@ func LoadConfig(config AppConfig) (AppConfig, error) {
94 func NewApp() *App {94 func NewApp() *App {
95 // Initialize AppConfig95 // 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 {
111111 return sendError(c, err)
112112 }
113113
114+ fmt.Println(cmdParams.PortMappings)
115+
114116 // Create the container
115117 _, err = docker.CreateContainer(cmdParams)
116118 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 container116 // 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 {
5151 networkNames = append(networkNames, networkName)
5252 }
5353
54- // Prepare Ports while using the containerInfo.Config.ExposedPorts
54+ // Prepare Ports (PortBindings and ExposedPorts)
5555 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+ }
6160 }
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+
6273 // Prepare Volumes (Mounts)
6374 volumeMappings := make([]string, 0)
6475 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.ExposedPorts54+ // 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 default58+ 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) {
200200 // Prepare port bindings
201201 portBindings := nat.PortMap{}
202202 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{
205206 {
206207 HostIP: "0.0.0.0",
207208 HostPort: portMapping.HostPort,
208209 },
209210 }
210-
211211 }
212212
213213 // Prepare labels for Traefik
@@ -200,14 +200,14 @@ func CreateContainer(cmdParams ContainerCommandParams) (string, error) {
200 // Prepare port bindings200 // 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 Traefik213 // Prepare labels for Traefik

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close