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.
This commit is contained in:
Ray Miller 2025-07-14 09:50:47 +01:00 committed by Ray Miller
parent 2099190211
commit 2dc179c175
3 changed files with 32 additions and 8 deletions

View File

@ -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

View File

@ -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,
}...)

View File

@ -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 {