diff --git a/cmd/ctr/run.go b/cmd/ctr/run.go index 5194dffa4..10f218aa4 100644 --- a/cmd/ctr/run.go +++ b/cmd/ctr/run.go @@ -89,6 +89,10 @@ var runCommand = cli.Command{ Name: "checkpoint", Usage: "provide the checkpoint digest to restore the container", }, + cli.StringFlag{ + Name: "cwd", + Usage: "specify the working directory of the process", + }, }, snapshotterFlags...), Action: func(context *cli.Context) error { var ( diff --git a/cmd/ctr/run_unix.go b/cmd/ctr/run_unix.go index e29f3f157..32d04dc82 100644 --- a/cmd/ctr/run_unix.go +++ b/cmd/ctr/run_unix.go @@ -107,6 +107,9 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli if len(args) > 0 { opts = append(opts, containerd.WithProcessArgs(args...)) } + if cwd := context.String("cwd"); cwd != "" { + opts = append(opts, containerd.WithProcessCwd(cwd)) + } if context.Bool("tty") { opts = append(opts, withTTY()) } diff --git a/cmd/ctr/run_windows.go b/cmd/ctr/run_windows.go index c3f3ae959..c764230fc 100644 --- a/cmd/ctr/run_windows.go +++ b/cmd/ctr/run_windows.go @@ -107,7 +107,9 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli if len(args) > 0 { opts = append(opts, containerd.WithProcessArgs(args...)) } - + if cwd := context.String("cwd"); cwd != "" { + opts = append(opts, containerd.WithProcessCwd(cwd)) + } return client.NewContainer(ctx, id, containerd.WithNewSpec(opts...), containerd.WithContainerLabels(labels), diff --git a/spec_opts.go b/spec_opts.go index 40cd2fa3f..e3edecbe9 100644 --- a/spec_opts.go +++ b/spec_opts.go @@ -19,6 +19,14 @@ func WithProcessArgs(args ...string) SpecOpts { } } +// WithProcessCwd replaces the current working directory on the generated spec +func WithProcessCwd(cwd string) SpecOpts { + return func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error { + s.Process.Cwd = cwd + return nil + } +} + // WithHostname sets the container's hostname func WithHostname(name string) SpecOpts { return func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error {