55 "fmt"
66 "log/slog"
77 "os"
8+ "regexp"
89
910 "github.com/arduino/arduino-app-cli/pkg/parser"
1011
@@ -15,6 +16,8 @@ import (
1516 "gopkg.in/yaml.v3"
1617)
1718
19+ var containerNameInvalidRegex = regexp .MustCompile (`[^a-zA-Z0-9]` )
20+
1821func ProvisionApp (ctx context.Context , docker * dockerClient.Client , app parser.App ) error {
1922 if err := pullBasePythonContainer (ctx , pythonImage ); err != nil {
2023 return fmt .Errorf ("provisioning failed to pull base image: %w" , err )
@@ -27,7 +30,7 @@ func ProvisionApp(ctx context.Context, docker *dockerClient.Client, app parser.A
2730 }, & container.HostConfig {
2831 Binds : []string {app .FullPath .String () + ":/app" },
2932 AutoRemove : true ,
30- }, nil , nil , app .Name )
33+ }, nil , nil , generateContainerName ( app .Name ) )
3134 if err != nil {
3235 return fmt .Errorf ("provisiong failed to create container: %w" , err )
3336 }
@@ -52,6 +55,17 @@ func ProvisionApp(ctx context.Context, docker *dockerClient.Client, app parser.A
5255 return generateMainComposeFile (ctx , app , pythonImage )
5356}
5457
58+ // Converts an arbitrary string to one that satisfies the container name requirement: [a-zA-Z0-9][a-zA-Z0-9_.-]
59+ // See the Docker Engine code here: https://github.com/moby/moby/blob/master/daemon/names/names.go#L6
60+ func generateContainerName (appName string ) string {
61+ result := containerNameInvalidRegex .ReplaceAllString (appName , "" )
62+
63+ if len (result ) < 1 {
64+ result = "c"
65+ }
66+ return result
67+ }
68+
5569func pullBasePythonContainer (ctx context.Context , pythonImage string ) error {
5670 process , err := paths .NewProcess (nil , "docker" , "pull" , pythonImage )
5771 if err != nil {
0 commit comments