detached 4e0dac8

From inputs to docker cmd line feature; OK

commit
4e0dac814d0db35c9d872440267f27de76f32e98
author
bnema · · 2023-09-26 18:26 UTC
parent
63805d6
signature
unsigned
3 files · +48 -35
M internal/gotemplate/models/html/fragments/createcontainer.gohtml +2 -2
@@ -108,7 +108,7 @@
108108 />
109109 </div>
110110
111- <!-- Environment Variables -->
111+ <!-- Environment Variables
112112 <div class="pb-4">
113113 <label class="block">Environment variables</label>
114114 <span class="text-gray-500">e.g. KEY=VALUE </span>
@@ -117,7 +117,7 @@
117117 name="environment_variables"
118118 class="w-full p-4 rounded-md"
119119 value="{{.EnvironmentVariables}}"
120- />
120+ /> -->
121121
122122 <!-- Restart: always/on-failure/only when stopped/never -->
123123 <div class="pb-4">
@@ -108,7 +108,7 @@
108 />108 />
109 </div>109 </div>
110 110
111- <!-- Environment Variables -->111+ <!-- Environment Variables
112 <div class="pb-4">112 <div class="pb-4">
113 <label class="block">Environment variables</label>113 <label class="block">Environment variables</label>
114 <span class="text-gray-500">e.g. KEY=VALUE </span>114 <span class="text-gray-500">e.g. KEY=VALUE </span>
@@ -117,7 +117,7 @@
117 name="environment_variables"117 name="environment_variables"
118 class="w-full p-4 rounded-md"118 class="w-full p-4 rounded-md"
119 value="{{.EnvironmentVariables}}"119 value="{{.EnvironmentVariables}}"
120- />120+ /> -->
121 121
122 <!-- Restart: always/on-failure/only when stopped/never -->122 <!-- Restart: always/on-failure/only when stopped/never -->
123 <div class="pb-4">123 <div class="pb-4">
M internal/gotemplate/render/cmdparams.go +9 -10
@@ -11,36 +11,35 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar
1111 fmt.Println("FromInputsToCmdParams:", inputs)
1212 // Populate the ContainerCommandParams struct directly from the map
1313 params := docker.ContainerCommandParams{
14- Https: inputs["container_protocol"] == "https",
14+ IsSSL: inputs["container_protocol"] == "https",
1515 ContainerName: inputs["container_name"],
16- ContainerHost: inputs["container_subdomain"],
16+ ServiceName: inputs["container_subdomain"],
1717 Domain: inputs["container_domain"],
1818 ImageName: inputs["image_name"],
1919 ImageID: inputs["image_id"],
2020 Ports: inputs["ports"],
2121 Restart: inputs["restart"],
22- Data: inputs["volumes"],
23- EnvVar: inputs["environment_variables"],
24- Network: "traefik",
22+ Volumes: inputs["volumes"],
23+ Network: ("traefik"),
2524 }
2625
2726 entryPoint := "web"
28- if params.Https {
27+ if params.IsSSL {
2928 entryPoint = "websecure"
3029 }
3130
3231 baseRouter := "traefik.http.routers." + params.ContainerName + "-router"
3332 baseService := "traefik.http.services." + params.ContainerName + "-service"
3433
35- params.TraefikLabels = []string{
34+ params.Labels = []string{
3635 "traefik.enable=true",
37- fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ContainerHost, params.Domain),
36+ fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ServiceName, params.Domain),
3837 fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint),
3938 fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports),
4039 }
4140
42- if params.Https {
43- params.TraefikLabels = append(params.TraefikLabels, fmt.Sprintf("%s.tls.certresolver=letsencrypt", baseRouter))
41+ if params.IsSSL {
42+ params.Labels = append(params.Labels, fmt.Sprintf("%s.tls.certresolver=letsencrypt", baseRouter))
4443 }
4544
4645 return params, nil
@@ -11,36 +11,35 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar
11 fmt.Println("FromInputsToCmdParams:", inputs)11 fmt.Println("FromInputsToCmdParams:", inputs)
12 // Populate the ContainerCommandParams struct directly from the map12 // Populate the ContainerCommandParams struct directly from the map
13 params := docker.ContainerCommandParams{13 params := docker.ContainerCommandParams{
14- Https: inputs["container_protocol"] == "https",14+ IsSSL: inputs["container_protocol"] == "https",
15 ContainerName: inputs["container_name"],15 ContainerName: inputs["container_name"],
16- ContainerHost: inputs["container_subdomain"],16+ ServiceName: inputs["container_subdomain"],
17 Domain: inputs["container_domain"],17 Domain: inputs["container_domain"],
18 ImageName: inputs["image_name"],18 ImageName: inputs["image_name"],
19 ImageID: inputs["image_id"],19 ImageID: inputs["image_id"],
20 Ports: inputs["ports"],20 Ports: inputs["ports"],
21 Restart: inputs["restart"],21 Restart: inputs["restart"],
22- Data: inputs["volumes"],22+ Volumes: inputs["volumes"],
23- EnvVar: inputs["environment_variables"],23+ Network: ("traefik"),
24- Network: "traefik",
25 }24 }
26 25
27 entryPoint := "web"26 entryPoint := "web"
28- if params.Https {27+ if params.IsSSL {
29 entryPoint = "websecure"28 entryPoint = "websecure"
30 }29 }
31 30
32 baseRouter := "traefik.http.routers." + params.ContainerName + "-router"31 baseRouter := "traefik.http.routers." + params.ContainerName + "-router"
33 baseService := "traefik.http.services." + params.ContainerName + "-service"32 baseService := "traefik.http.services." + params.ContainerName + "-service"
34 33
35- params.TraefikLabels = []string{34+ params.Labels = []string{
36 "traefik.enable=true",35 "traefik.enable=true",
37- fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ContainerHost, params.Domain),36+ fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ServiceName, params.Domain),
38 fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint),37 fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint),
39 fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports),38 fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports),
40 }39 }
41 40
42- if params.Https {41+ if params.IsSSL {
43- params.TraefikLabels = append(params.TraefikLabels, fmt.Sprintf("%s.tls.certresolver=letsencrypt", baseRouter))42+ params.Labels = append(params.Labels, fmt.Sprintf("%s.tls.certresolver=letsencrypt", baseRouter))
44 }43 }
45 44
46 return params, nil45 return params, nil
M pkg/utils/docker/containers.go +37 -23
@@ -8,7 +8,7 @@ import (
88
99 "github.com/docker/docker/api/types"
1010 "github.com/docker/docker/api/types/container"
11- "github.com/docker/docker/client"
11+ "github.com/docker/docker/api/types/network"
1212 "github.com/docker/go-connections/nat"
1313 )
1414
@@ -16,13 +16,14 @@ type ContainerCommandParams struct {
1616 ContainerName string
1717 ContainerHost string
1818 Domain string
19- Https bool
19+ ServiceName string
20+ IsSSL bool
2021 EnvVar string
2122 ImageName string
2223 ImageID string
2324 Ports string
24- Data string
25- TraefikLabels []string
25+ Volumes string
26+ Labels []string
2627 Network string
2728 Restart string
2829 }
@@ -109,22 +110,11 @@ func StartContainer(containerID string) error {
109110
110111 // CreateContainer creates a container with the given parameters
111112 func CreateContainer(cmdParams ContainerCommandParams) error {
112- // Initialize Docker client
113- dockerCli, err := client.NewClientWithOpts(client.FromEnv)
114- if err != nil {
115- return err
116- }
113+ // Check if the Docker client has been initialized
114+ CheckIfInitialized()
117115
118- // Prepare container port bindings
116+ // Prepare port bindings
119117 portBindings := nat.PortMap{}
120- hostConfig := &container.HostConfig{
121- PortBindings: portBindings,
122- RestartPolicy: container.RestartPolicy{
123- Name: cmdParams.Restart,
124- },
125- }
126-
127- // Define port mapping
128118 parts := strings.Split(cmdParams.Ports, ":")
129119 if len(parts) == 2 {
130120 portBindings[nat.Port(parts[1])] = []nat.PortBinding{
@@ -132,23 +122,47 @@ func CreateContainer(cmdParams ContainerCommandParams) error {
132122 }
133123 }
134124
125+ // Prepare labels for Traefik
126+ labels := map[string]string{}
127+ for _, label := range cmdParams.Labels {
128+ keyValue := strings.Split(label, "=")
129+ if len(keyValue) == 2 {
130+ labels[keyValue[0]] = keyValue[1]
131+ }
132+ }
133+
135134 // Create container
136135 resp, err := dockerCli.ContainerCreate(
137136 context.Background(),
138137 &container.Config{
139138 Image: cmdParams.ImageName,
140- Labels: map[string]string{},
139+ Labels: labels,
140+ },
141+ &container.HostConfig{
142+ PortBindings: portBindings,
143+ Binds: []string{cmdParams.Volumes},
144+ RestartPolicy: container.RestartPolicy{
145+ Name: cmdParams.Restart,
146+ },
141147 },
142- hostConfig,
143- nil, nil,
148+ &network.NetworkingConfig{
149+ EndpointsConfig: map[string]*network.EndpointSettings{
150+ cmdParams.Network: {},
151+ },
152+ },
153+ nil,
144154 cmdParams.ContainerName,
145155 )
146156 if err != nil {
147157 return err
148158 }
149159
150- // Start container
151- err = dockerCli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{})
160+ // Start the container
161+ err = dockerCli.ContainerStart(
162+ context.Background(),
163+ resp.ID,
164+ types.ContainerStartOptions{},
165+ )
152166 if err != nil {
153167 return err
154168 }
@@ -8,7 +8,7 @@ import (
8 8
9 "github.com/docker/docker/api/types"9 "github.com/docker/docker/api/types"
10 "github.com/docker/docker/api/types/container"10 "github.com/docker/docker/api/types/container"
11- "github.com/docker/docker/client"11+ "github.com/docker/docker/api/types/network"
12 "github.com/docker/go-connections/nat"12 "github.com/docker/go-connections/nat"
13 )13 )
14 14
@@ -16,13 +16,14 @@ type ContainerCommandParams struct {
16 ContainerName string16 ContainerName string
17 ContainerHost string17 ContainerHost string
18 Domain string18 Domain string
19- Https bool19+ ServiceName string
20+ IsSSL bool
20 EnvVar string21 EnvVar string
21 ImageName string22 ImageName string
22 ImageID string23 ImageID string
23 Ports string24 Ports string
24- Data string25+ Volumes string
25- TraefikLabels []string26+ Labels []string
26 Network string27 Network string
27 Restart string28 Restart string
28 }29 }
@@ -109,22 +110,11 @@ func StartContainer(containerID string) error {
109 110
110 // CreateContainer creates a container with the given parameters111 // CreateContainer creates a container with the given parameters
111 func CreateContainer(cmdParams ContainerCommandParams) error {112 func CreateContainer(cmdParams ContainerCommandParams) error {
112- // Initialize Docker client113+ // Check if the Docker client has been initialized
113- dockerCli, err := client.NewClientWithOpts(client.FromEnv)114+ CheckIfInitialized()
114- if err != nil {
115- return err
116- }
117 115
118- // Prepare container port bindings116+ // Prepare port bindings
119 portBindings := nat.PortMap{}117 portBindings := nat.PortMap{}
120- hostConfig := &container.HostConfig{
121- PortBindings: portBindings,
122- RestartPolicy: container.RestartPolicy{
123- Name: cmdParams.Restart,
124- },
125- }
126-
127- // Define port mapping
128 parts := strings.Split(cmdParams.Ports, ":")118 parts := strings.Split(cmdParams.Ports, ":")
129 if len(parts) == 2 {119 if len(parts) == 2 {
130 portBindings[nat.Port(parts[1])] = []nat.PortBinding{120 portBindings[nat.Port(parts[1])] = []nat.PortBinding{
@@ -132,23 +122,47 @@ func CreateContainer(cmdParams ContainerCommandParams) error {
132 }122 }
133 }123 }
134 124
125+ // Prepare labels for Traefik
126+ labels := map[string]string{}
127+ for _, label := range cmdParams.Labels {
128+ keyValue := strings.Split(label, "=")
129+ if len(keyValue) == 2 {
130+ labels[keyValue[0]] = keyValue[1]
131+ }
132+ }
133+
135 // Create container134 // Create container
136 resp, err := dockerCli.ContainerCreate(135 resp, err := dockerCli.ContainerCreate(
137 context.Background(),136 context.Background(),
138 &container.Config{137 &container.Config{
139 Image: cmdParams.ImageName,138 Image: cmdParams.ImageName,
140- Labels: map[string]string{},139+ Labels: labels,
140+ },
141+ &container.HostConfig{
142+ PortBindings: portBindings,
143+ Binds: []string{cmdParams.Volumes},
144+ RestartPolicy: container.RestartPolicy{
145+ Name: cmdParams.Restart,
146+ },
141 },147 },
142- hostConfig,148+ &network.NetworkingConfig{
143- nil, nil,149+ EndpointsConfig: map[string]*network.EndpointSettings{
150+ cmdParams.Network: {},
151+ },
152+ },
153+ nil,
144 cmdParams.ContainerName,154 cmdParams.ContainerName,
145 )155 )
146 if err != nil {156 if err != nil {
147 return err157 return err
148 }158 }
149 159
150- // Start container160+ // Start the container
151- err = dockerCli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{})161+ err = dockerCli.ContainerStart(
162+ context.Background(),
163+ resp.ID,
164+ types.ContainerStartOptions{},
165+ )
152 if err != nil {166 if err != nil {
153 return err167 return err
154 }168 }

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