Skip to content

Commit

Permalink
feat(cli): support running with rootless docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone authored and 06kellyjac committed Dec 21, 2023
1 parent 1b9c39e commit 69cca81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 4 additions & 1 deletion internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var name, bucket string
var dev bool
var dev, rootless bool

var configCmd = &cobra.Command{
Use: "config",
Expand All @@ -33,13 +33,16 @@ var configCmd = &cobra.Command{
cfg.Cli.Dev = false
cfg.Container.Image = "controlplane/simulator:latest"
}

cfg.Container.Rootless = rootless
},
}

func init() {
configCmd.PersistentFlags().StringVar(&name, "name", "simulator", "the name for the infrastructure")
configCmd.PersistentFlags().StringVar(&bucket, "bucket", "", "the s3 bucket used for storage")
configCmd.PersistentFlags().BoolVar(&dev, "dev", false, "developer mode")
configCmd.PersistentFlags().BoolVar(&rootless, "rootless", false, "docker running in rootless mode")

simulatorCmd.AddCommand(configCmd)
}
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Config struct {
} `yaml:"cli,omitempty"`

Container struct {
Image string `yaml:"image"`
Image string `yaml:"image"`
Rootless bool `yaml:"rootless,omitempty"`
} `yaml:"container"`
}

Expand Down
25 changes: 16 additions & 9 deletions internal/container/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,22 @@ func (r simulator) Run(ctx context.Context, command []string) error {
}...)
}

containerConfig := &container.Config{
Image: r.Config.Container.Image,
Env: aws.Env,
Cmd: command,
Tty: true,
AttachStdout: true,
AttachStderr: true,
}

if r.Config.Container.Rootless {
// map to host user for directory access
containerConfig.User = "0:0"
}

cont, err := cli.ContainerCreate(ctx,
&container.Config{
Image: r.Config.Container.Image,
Env: aws.Env,
Cmd: command,
Tty: true,
AttachStdout: true,
AttachStderr: true,
},
containerConfig,
&container.HostConfig{
Mounts: mounts,
},
Expand Down Expand Up @@ -137,7 +144,7 @@ func (r simulator) Run(ctx context.Context, command []string) error {

err = cli.ContainerStart(ctx, cont.ID, types.ContainerStartOptions{})
if err != nil {
return StartFailed
return errors.Join(StartFailed, err)
}

var wg sync.WaitGroup
Expand Down

0 comments on commit 69cca81

Please sign in to comment.