diff --git a/src/cmd/create.go b/src/cmd/create.go index b4e923651..f8d175ea7 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -45,6 +45,7 @@ var ( authFile string container string distro string + hostname string image string release string } @@ -85,6 +86,12 @@ func init() { "", "Create a toolbox container for a different operating system distribution than the host") + flags.StringVarP(&createFlags.hostname, + "hostname", + "h", + "toolbox", + "Assign a custom hostname to the toolbox container") + flags.StringVarP(&createFlags.image, "image", "i", @@ -159,6 +166,12 @@ func create(cmd *cobra.Command, args []string) error { } } + var hostname string + + if createFlags.hostname != "" { + hostname = createFlags.hostname + } + distro, err := utils.ResolveDistro(createFlags.distro) if err != nil { err := createErrorInvalidDistro() @@ -185,14 +198,14 @@ func create(cmd *cobra.Command, args []string) error { return err } - if err := createContainer(container, image, release, true); err != nil { + if err := createContainer(container, hostname, image, release, true); err != nil { return err } return nil } -func createContainer(container, image, release string, showCommandToEnter bool) error { +func createContainer(container, hostname, image, release string, showCommandToEnter bool) error { if container == "" { panic("container not specified") } @@ -419,7 +432,7 @@ func createContainer(container, image, release string, showCommandToEnter bool) createArgs = append(createArgs, xdgRuntimeDirEnv...) createArgs = append(createArgs, []string{ - "--hostname", "toolbox", + "--hostname", hostname, "--ipc", "host", "--label", "com.github.containers.toolbox=true", }...) diff --git a/src/cmd/run.go b/src/cmd/run.go index d65143a6a..5f2cb6b41 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -228,7 +228,13 @@ func runCommand(container string, return nil } - if err := createContainer(container, image, release, false); err != nil { + var hostname string + + if createFlags.hostname != "" { + hostname = createFlags.hostname + } + + if err := createContainer(container, hostname, image, release, false); err != nil { return err } } else if containersCount == 1 && defaultContainer {