Skip to content

Commit 9f1ceaa

Browse files
author
mirkobrombin
committed
feat: enhance DockerStandardPlugin to use unique container names
1 parent 08dfdf9 commit 9f1ceaa

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

plugins/docker_standard.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,13 @@ func (d *DockerStandardPlugin) HandleRequest(w http.ResponseWriter, r *http.Requ
124124
}
125125
}
126126
// If proxy path is "/" use the container's root.
127+
targetURL := fmt.Sprintf("http://0.0.0.0:%s", state.config.ContainerPort)
127128
if len(state.config.ProxyPaths) == 1 && state.config.ProxyPaths[0] == "/" {
128-
targetURL := fmt.Sprintf("http://0.0.0.0:%s", state.config.ContainerPort)
129129
d.proxyToContainer(targetURL, w, r)
130130
return true
131131
}
132132
for _, prefix := range state.config.ProxyPaths {
133133
if strings.HasPrefix(r.URL.Path, prefix) {
134-
targetURL := fmt.Sprintf("http://0.0.0.0:%s", state.config.ContainerPort)
135134
if r.URL.RawQuery != "" {
136135
targetURL += "?" + r.URL.RawQuery
137136
}
@@ -167,12 +166,17 @@ func (d *DockerStandardPlugin) ensureContainer(domain string) error {
167166
if state.containerID != "" {
168167
return nil
169168
}
170-
existingID, err := GetRunningContainer(state.config.CLICommand, state.config.DockerfilePath, state.config.ImageName)
171-
if err == nil && existingID != "" {
172-
state.containerID = existingID
169+
170+
// Generate unique container name using domain and container port.
171+
containerName := fmt.Sprintf("goup_%s_%s", domain, state.config.ContainerPort)
172+
173+
// Checking if a container with this unique name is already running.
174+
existingID, err := RunDockerCLI(state.config.CLICommand, state.config.DockerfilePath, "ps", "--filter", fmt.Sprintf("name=%s", containerName), "--format", "{{.ID}}")
175+
if err == nil && strings.TrimSpace(existingID) != "" {
176+
state.containerID = strings.TrimSpace(existingID)
173177
return nil
174178
}
175-
d.DomainLogger.Infof("[DockerStandardPlugin] Starting container for domain=%s", domain)
179+
d.DomainLogger.Infof("[DockerStandardPlugin] Starting container for domain=%s with tag %s", domain, containerName)
176180
cliCmd := state.config.CLICommand
177181
if cliCmd == "" {
178182
cliCmd = "docker"
@@ -207,7 +211,7 @@ func (d *DockerStandardPlugin) ensureContainer(domain string) error {
207211
}
208212
d.PluginLogger.Infof("Pull output: %s", pullOutput)
209213
}
210-
runArgs := []string{"run", "-d", "-p", fmt.Sprintf("%s:%s", state.config.ContainerPort, state.config.ContainerPort)}
214+
runArgs := []string{"run", "-d", "--name", containerName, "-p", fmt.Sprintf("%s:%s", state.config.ContainerPort, state.config.ContainerPort)}
211215
runArgs = append(runArgs, state.config.RunArgs...)
212216
runArgs = append(runArgs, state.config.ImageName)
213217
d.PluginLogger.Infof("[DockerStandardPlugin] Running container with command: %s %s", cliCmd, strings.Join(runArgs, " "))

0 commit comments

Comments
 (0)