Skip to content

Commit e25292e

Browse files
authored
Merge pull request #1203 from hongil0316/docker_compose_project_name_fix
Fix project-name so it doesn't contain special characters for docker-…
2 parents 3f5f355 + 2f193bf commit e25292e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/docker/docker_compose.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package docker
22

33
import (
4+
"regexp"
45
"strings"
56

67
"github.com/gruntwork-io/terratest/modules/logger"
@@ -62,7 +63,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
6263
if result.ExitCode == 0 {
6364
cmd = shell.Command{
6465
Command: "docker",
65-
Args: append([]string{"compose", "--project-name", strings.ToLower(t.Name())}, args...),
66+
Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
6667
WorkingDir: options.WorkingDir,
6768
Env: options.EnvVars,
6869
Logger: options.Logger,
@@ -72,7 +73,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
7273
Command: "docker-compose",
7374
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
7475
// up in the same project and end up conflicting with each other.
75-
Args: append([]string{"--project-name", strings.ToLower(t.Name())}, args...),
76+
Args: append([]string{"--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
7677
WorkingDir: options.WorkingDir,
7778
Env: options.EnvVars,
7879
Logger: options.Logger,
@@ -84,3 +85,9 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
8485
}
8586
return shell.RunCommandAndGetOutputE(t, cmd)
8687
}
88+
89+
// Note: docker-compose command doesn't like lower case or special characters, other than -.
90+
func generateValidDockerComposeProjectName(str string) string {
91+
lower_str := strings.ToLower(str)
92+
return regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(lower_str, "-")
93+
}

0 commit comments

Comments
 (0)