11package docker
22
33import (
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