diff --git a/modules/docker/docker_compose.go b/modules/docker/docker_compose.go index 2b48ffbcc..d90ccff36 100644 --- a/modules/docker/docker_compose.go +++ b/modules/docker/docker_compose.go @@ -1,6 +1,7 @@ package docker import ( + "regexp" "strings" "github.com/gruntwork-io/terratest/modules/logger" @@ -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, @@ -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, @@ -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, "-") +}