From 2dc179c1757ff88dab7c9e02fe540c879930d720 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Mon, 14 Jul 2025 09:50:47 +0100 Subject: [PATCH] cmd/create: extra volume and environment variables Add --volume (-V) and --env (-E) options to the create command to allow extra volumes and environment variables to be specified at container create time. --- doc/toolbox-create.1.md | 2 ++ src/cmd/create.go | 36 +++++++++++++++++++++++++++++------- src/cmd/run.go | 2 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/doc/toolbox-create.1.md b/doc/toolbox-create.1.md index b2dacb303..49bf8f648 100644 --- a/doc/toolbox-create.1.md +++ b/doc/toolbox-create.1.md @@ -8,6 +8,8 @@ toolbox\-create - Create a new Toolbx container [*--distro DISTRO* | *-d DISTRO*] [*--image NAME* | *-i NAME*] [*--release RELEASE* | *-r RELEASE*] + [*--volume* | *-V* [SOURCE-VOLUME|HOST-DIR:]CONTAINER-DIR[:OPTIONS]] + [*--env* | *-E* NAME[=VALUE]] [*CONTAINER*] ## DESCRIPTION diff --git a/src/cmd/create.go b/src/cmd/create.go index 531721b60..62731e1df 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -49,11 +49,13 @@ const ( var ( createFlags struct { - authFile string - container string - distro string - image string - release string + authFile string + container string + distro string + image string + release string + extraVolumes []string + extraEnvVars []string } createToolboxShMounts = []struct { @@ -104,6 +106,18 @@ func init() { "", "Create a Toolbx container for a different operating system release than the host") + flags.StringArrayVarP(&createFlags.extraVolumes, + "volume", + "V", + nil, + "Mount an additional volume in the toolbox container (may be repeated)") + + flags.StringArrayVarP(&createFlags.extraEnvVars, + "env", + "E", + nil, + "Define an additional environment variable for the container (may be repeated)") + createCmd.SetHelpFunc(createHelp) if err := createCmd.RegisterFlagCompletionFunc("distro", completionDistroNames); err != nil { @@ -180,14 +194,14 @@ func create(cmd *cobra.Command, args []string) error { return err } - if err := createContainer(container, image, release, createFlags.authFile, true); err != nil { + if err := createContainer(container, image, release, createFlags.authFile, createFlags.extraVolumes, createFlags.extraEnvVars, true); err != nil { return err } return nil } -func createContainer(container, image, release, authFile string, showCommandToEnter bool) error { +func createContainer(container, image, release, authFile string, extraVolumes []string, extraEnvVars []string, showCommandToEnter bool) error { if container == "" { panic("container not specified") } @@ -473,6 +487,14 @@ func createContainer(container, image, release, authFile string, showCommandToEn createArgs = append(createArgs, runMediaMount...) createArgs = append(createArgs, toolboxShMount...) + for _, v := range extraVolumes { + createArgs = append(createArgs, "--volume", v) + } + + for _, e := range extraEnvVars { + createArgs = append(createArgs, "--env", e) + } + createArgs = append(createArgs, []string{ imageFull, }...) diff --git a/src/cmd/run.go b/src/cmd/run.go index 389ea1615..808001796 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -222,7 +222,7 @@ func runCommand(container string, return nil } - if err := createContainer(container, image, release, "", false); err != nil { + if err := createContainer(container, image, release, "", nil, nil, false); err != nil { return err } } else if containersCount == 1 && defaultContainer {