From inputs to docker cmd line feature; OK
- commit
- c24ebd26c214170bdf8346a509b810187d5a7942
- author
- bnema · · 2023-09-26 18:26 UTC
- parent
- 60c12cd
- signature
- unsigned
3 files · +48 -35
M internal/gotemplate/models/html/fragments/createcontainer.gohtml +2 -2
| @@ -108,7 +108,7 @@ | |||
| 108 | 108 | /> | |
| 109 | 109 | </div> | |
| 110 | 110 | ||
| 111 | - | <!-- Environment Variables --> | |
| 111 | + | <!-- Environment Variables | |
| 112 | 112 | <div class="pb-4"> | |
| 113 | 113 | <label class="block">Environment variables</label> | |
| 114 | 114 | <span class="text-gray-500">e.g. KEY=VALUE </span> | |
| @@ -117,7 +117,7 @@ | |||
| 117 | 117 | name="environment_variables" | |
| 118 | 118 | class="w-full p-4 rounded-md" | |
| 119 | 119 | value="{{.EnvironmentVariables}}" | |
| 120 | - | /> | |
| 120 | + | /> --> | |
| 121 | 121 | ||
| 122 | 122 | <!-- Restart: always/on-failure/only when stopped/never --> | |
| 123 | 123 | <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 | |||
| 11 | 11 | fmt.Println("FromInputsToCmdParams:", inputs) | |
| 12 | 12 | // Populate the ContainerCommandParams struct directly from the map | |
| 13 | 13 | params := docker.ContainerCommandParams{ | |
| 14 | - | Https: inputs["container_protocol"] == "https", | |
| 14 | + | IsSSL: inputs["container_protocol"] == "https", | |
| 15 | 15 | ContainerName: inputs["container_name"], | |
| 16 | - | ContainerHost: inputs["container_subdomain"], | |
| 16 | + | ServiceName: inputs["container_subdomain"], | |
| 17 | 17 | Domain: inputs["container_domain"], | |
| 18 | 18 | ImageName: inputs["image_name"], | |
| 19 | 19 | ImageID: inputs["image_id"], | |
| 20 | 20 | Ports: inputs["ports"], | |
| 21 | 21 | Restart: inputs["restart"], | |
| 22 | - | Data: inputs["volumes"], | |
| 23 | - | EnvVar: inputs["environment_variables"], | |
| 24 | - | Network: "traefik", | |
| 22 | + | Volumes: inputs["volumes"], | |
| 23 | + | Network: ("traefik"), | |
| 25 | 24 | } | |
| 26 | 25 | ||
| 27 | 26 | entryPoint := "web" | |
| 28 | - | if params.Https { | |
| 27 | + | if params.IsSSL { | |
| 29 | 28 | entryPoint = "websecure" | |
| 30 | 29 | } | |
| 31 | 30 | ||
| 32 | 31 | baseRouter := "traefik.http.routers." + params.ContainerName + "-router" | |
| 33 | 32 | baseService := "traefik.http.services." + params.ContainerName + "-service" | |
| 34 | 33 | ||
| 35 | - | params.TraefikLabels = []string{ | |
| 34 | + | params.Labels = []string{ | |
| 36 | 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 | 37 | fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint), | |
| 39 | 38 | fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports), | |
| 40 | 39 | } | |
| 41 | 40 | ||
| 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)) | |
| 44 | 43 | } | |
| 45 | 44 | ||
| 46 | 45 | 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 map | 12 | // 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, nil | 45 | return params, nil | ||
M pkg/utils/docker/containers.go +37 -23
| @@ -8,7 +8,7 @@ import ( | |||
| 8 | 8 | ||
| 9 | 9 | "github.com/docker/docker/api/types" | |
| 10 | 10 | "github.com/docker/docker/api/types/container" | |
| 11 | - | "github.com/docker/docker/client" | |
| 11 | + | "github.com/docker/docker/api/types/network" | |
| 12 | 12 | "github.com/docker/go-connections/nat" | |
| 13 | 13 | ) | |
| 14 | 14 | ||
| @@ -16,13 +16,14 @@ type ContainerCommandParams struct { | |||
| 16 | 16 | ContainerName string | |
| 17 | 17 | ContainerHost string | |
| 18 | 18 | Domain string | |
| 19 | - | Https bool | |
| 19 | + | ServiceName string | |
| 20 | + | IsSSL bool | |
| 20 | 21 | EnvVar string | |
| 21 | 22 | ImageName string | |
| 22 | 23 | ImageID string | |
| 23 | 24 | Ports string | |
| 24 | - | Data string | |
| 25 | - | TraefikLabels []string | |
| 25 | + | Volumes string | |
| 26 | + | Labels []string | |
| 26 | 27 | Network string | |
| 27 | 28 | Restart string | |
| 28 | 29 | } | |
| @@ -109,22 +110,11 @@ func StartContainer(containerID string) error { | |||
| 109 | 110 | ||
| 110 | 111 | // CreateContainer creates a container with the given parameters | |
| 111 | 112 | 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() | |
| 117 | 115 | ||
| 118 | - | // Prepare container port bindings | |
| 116 | + | // Prepare port bindings | |
| 119 | 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 | 118 | parts := strings.Split(cmdParams.Ports, ":") | |
| 129 | 119 | if len(parts) == 2 { | |
| 130 | 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 | 134 | // Create container | |
| 136 | 135 | resp, err := dockerCli.ContainerCreate( | |
| 137 | 136 | context.Background(), | |
| 138 | 137 | &container.Config{ | |
| 139 | 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, | |
| 143 | - | nil, nil, | |
| 148 | + | &network.NetworkingConfig{ | |
| 149 | + | EndpointsConfig: map[string]*network.EndpointSettings{ | |
| 150 | + | cmdParams.Network: {}, | |
| 151 | + | }, | |
| 152 | + | }, | |
| 153 | + | nil, | |
| 144 | 154 | cmdParams.ContainerName, | |
| 145 | 155 | ) | |
| 146 | 156 | if err != nil { | |
| 147 | 157 | return err | |
| 148 | 158 | } | |
| 149 | 159 | ||
| 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 | + | ) | |
| 152 | 166 | if err != nil { | |
| 153 | 167 | return err | |
| 154 | 168 | } | |
| @@ -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 string | 16 | ContainerName string | ||
| 17 | ContainerHost string | 17 | ContainerHost string | ||
| 18 | Domain string | 18 | Domain string | ||
| 19 | - | Https bool | 19 | + | ServiceName string |
| 20 | + | IsSSL bool | |||
| 20 | EnvVar string | 21 | EnvVar string | ||
| 21 | ImageName string | 22 | ImageName string | ||
| 22 | ImageID string | 23 | ImageID string | ||
| 23 | Ports string | 24 | Ports string | ||
| 24 | - | Data string | 25 | + | Volumes string |
| 25 | - | TraefikLabels []string | 26 | + | Labels []string |
| 26 | Network string | 27 | Network string | ||
| 27 | Restart string | 28 | Restart string | ||
| 28 | } | 29 | } | ||
| @@ -109,22 +110,11 @@ func StartContainer(containerID string) error { | |||||
| 109 | 110 | ||||
| 110 | // CreateContainer creates a container with the given parameters | 111 | // CreateContainer creates a container with the given parameters | ||
| 111 | func CreateContainer(cmdParams ContainerCommandParams) error { | 112 | func CreateContainer(cmdParams ContainerCommandParams) error { | ||
| 112 | - | // Initialize Docker client | 113 | + | // 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 bindings | 116 | + | // 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 container | 134 | // 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 err | 157 | return err | ||
| 148 | } | 158 | } | ||
| 149 | 159 | ||||
| 150 | - | // Start container | 160 | + | // 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 err | 167 | return err | ||
| 154 | } | 168 | } | ||