Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Metadata flags:
- :whale: `-l, --label`: Set meta data on a container
- :whale: `--label-file`: Read in a line delimited file of labels
- :whale: `--cidfile`: Write the container ID to the file
- :nerd_face: `--pidfile`: file path to write the task's pid. The CLI syntax conforms to Podman convention.

Shared memory flags:
- :whale: `--shm-size`: Size of `/dev/shm`
Expand Down
21 changes: 19 additions & 2 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ var runCommand = &cli.Command{
Name: "shm-size",
Usage: "Size of /dev/shm",
},
&cli.StringFlag{
Name: "pidfile",
Usage: "file path to write the task's pid",
},
},
}

Expand Down Expand Up @@ -526,7 +530,13 @@ func runAction(clicontext *cli.Context) error {
return err
}
}
ilOpt, err := withInternalLabels(ns, name, hostname, stateDir, netSlice, ports, logURI, anonVolumes)

var pidFile string
if clicontext.IsSet("pidfile") {
pidFile = clicontext.String("pidfile")
}

ilOpt, err := withInternalLabels(ns, name, hostname, stateDir, netSlice, ports, logURI, anonVolumes, pidFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -589,9 +599,11 @@ func runAction(clicontext *cli.Context) error {
return err
}
}

if err := task.Start(ctx); err != nil {
return err
}

if flagD {
fmt.Fprintf(clicontext.App.Writer, "%s\n", id)
return nil
Expand Down Expand Up @@ -800,7 +812,7 @@ func withContainerLabels(clicontext *cli.Context) ([]containerd.NewContainerOpts
return []containerd.NewContainerOpts{o}, nil
}

func withInternalLabels(ns, name, hostname, containerStateDir string, networks []string, ports []gocni.PortMapping, logURI string, anonVolumes []string) (containerd.NewContainerOpts, error) {
func withInternalLabels(ns, name, hostname, containerStateDir string, networks []string, ports []gocni.PortMapping, logURI string, anonVolumes []string, pidFile string) (containerd.NewContainerOpts, error) {
m := make(map[string]string)
m[labels.Namespace] = ns
if name != "" {
Expand Down Expand Up @@ -830,6 +842,11 @@ func withInternalLabels(ns, name, hostname, containerStateDir string, networks [
}
m[labels.AnonymousVolumes] = string(anonVolumeJSON)
}

if pidFile != "" {
m[labels.PIDFile] = pidFile
}

return containerd.WithAdditionalContainerLabels(m), nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const (
// LogURI is the log URI
LogURI = Prefix + "log-uri"

// PIDFile is the `nerdctl run --pidfile`
// (CLI flag is "pidfile", not "pid-file", for Podman compatibility)
PIDFile = Prefix + "pid-file"

// AnonymousVolumes is a JSON-marshalled string of []string
AnonymousVolumes = Prefix + "anonymous-volumes"
)
8 changes: 8 additions & 0 deletions pkg/ocihook/ocihook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os"
"path/filepath"

"github.com/containerd/containerd/cmd/ctr/commands"

pkgapparmor "github.com/containerd/containerd/pkg/apparmor"
gocni "github.com/containerd/go-cni"
"github.com/containerd/nerdctl/pkg/dnsutil/hostsstore"
Expand Down Expand Up @@ -150,6 +152,12 @@ func newHandlerOpts(state *specs.State, dataStore, cniPath, cniNetconfPath strin
return nil, errors.Errorf("unexpected network type %v", netType)
}

if pidFile := o.state.Annotations[labels.PIDFile]; pidFile != "" {
if err := commands.WritePidFile(pidFile, state.Pid); err != nil {
return nil, err
}
}

if portsJSON := o.state.Annotations[labels.Ports]; portsJSON != "" {
if err := json.Unmarshal([]byte(portsJSON), &o.ports); err != nil {
return nil, err
Expand Down