Skip to content

Commit

Permalink
Merge pull request #1203 from hongil0316/docker_compose_project_name_fix
Browse files Browse the repository at this point in the history
Fix project-name so it doesn't contain special characters for docker-…
  • Loading branch information
hongil0316 committed Nov 18, 2022
2 parents 3f5f355 + 2f193bf commit e25292e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/docker/docker_compose.go
@@ -1,6 +1,7 @@
package docker

import (
"regexp"
"strings"

"github.com/gruntwork-io/terratest/modules/logger"
Expand Down Expand Up @@ -62,7 +63,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
if result.ExitCode == 0 {
cmd = shell.Command{
Command: "docker",
Args: append([]string{"compose", "--project-name", strings.ToLower(t.Name())}, args...),
Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
Expand All @@ -72,7 +73,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
Command: "docker-compose",
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
// up in the same project and end up conflicting with each other.
Args: append([]string{"--project-name", strings.ToLower(t.Name())}, args...),
Args: append([]string{"--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
Expand All @@ -84,3 +85,9 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
}
return shell.RunCommandAndGetOutputE(t, cmd)
}

// Note: docker-compose command doesn't like lower case or special characters, other than -.
func generateValidDockerComposeProjectName(str string) string {
lower_str := strings.ToLower(str)
return regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(lower_str, "-")
}

0 comments on commit e25292e

Please sign in to comment.