Skip to content

Commit

Permalink
add nerdctl run --shm-size <SIZE>
Browse files Browse the repository at this point in the history
Signed-off-by: Yuchen Cheng <rudeigerc@gmail.com>
  • Loading branch information
rudeigerc committed Jun 5, 2021
1 parent e3e7ee9 commit be6d82e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ Metadata flags:
- :whale: `--label-file`: Read in a line delimited file of labels
- :whale: `--cidfile`: Write the container ID to the file

Shared memory flags:
- :whale: `--shm-size`: Size of `/dev/shm`

Other `docker run` flags are on plan but unimplemented yet.
<details>
<summary> Clicke here to show all the `docker run` flags (Docker 20.10)</summary>
Expand Down
14 changes: 14 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/taskutil"
"github.com/docker/cli/opts"
"github.com/docker/go-units"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -231,6 +232,11 @@ var runCommand = &cli.Command{
Name: "cidfile",
Usage: "Write the container ID to the file",
},
// shared memory flags
&cli.StringFlag{
Name: "shm-size",
Usage: "Size of /dev/shm",
},
},
}

Expand Down Expand Up @@ -445,6 +451,14 @@ func runAction(clicontext *cli.Context) error {
opts = append(opts, privilegedOpts...)
}

if shmSize := clicontext.String("shm-size"); len(shmSize) > 0 {
shmBytes, err := units.RAMInBytes(shmSize)
if err != nil {
return err
}
opts = append(opts, oci.WithDevShmSize(shmBytes/1024))
}

rtCOpts, err := generateRuntimeCOpts(clicontext)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,10 @@ func TestRunCIDFile(t *testing.T) {

base.Cmd("run", "--rm", "--cidfile", fileName, testutil.AlpineImage).AssertFail()
}

func TestRunShmSize(t *testing.T) {
base := testutil.NewBase(t)
const shmSize = "32m"

base.Cmd("run", "--rm", "--shm-size", shmSize, testutil.AlpineImage, "/bin/grep", "shm", "/proc/self/mounts").AssertOutContains("size=32768k")
}

0 comments on commit be6d82e

Please sign in to comment.