diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go index 7fd311db429..3a76732b969 100644 --- a/libcontainer/configs/config.go +++ b/libcontainer/configs/config.go @@ -183,6 +183,9 @@ type Hooks struct { // but before the user supplied command is executed from init. Prestart []Hook + // Poststart commands are executed after the container init process starts. + Poststart []Hook + // Poststop commands are executed after the container init process exits. Poststop []Hook } diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 94cb8979a15..69d43e8594e 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -183,6 +183,22 @@ func (c *linuxContainer) Start(process *Process) error { if doInit { c.updateState(parent) } + if c.config.Hooks != nil { + s := configs.HookState{ + Version: c.config.Version, + ID: c.id, + Pid: parent.pid(), + Root: c.config.Rootfs, + } + for _, hook := range c.config.Hooks.Poststart { + if err := hook.Run(s); err != nil { + if err := parent.terminate(); err != nil { + logrus.Warn(err) + } + return newSystemError(err) + } + } + } return nil } diff --git a/spec.go b/spec.go index 69cf47b799a..f679a0aada9 100644 --- a/spec.go +++ b/spec.go @@ -712,6 +712,14 @@ func createHooks(rspec *specs.LinuxRuntimeSpec, config *configs.Config) { } config.Hooks.Prestart = append(config.Hooks.Prestart, configs.NewCommandHook(cmd)) } + for _, h := range rspec.Hooks.Poststart { + cmd := configs.Command{ + Path: h.Path, + Args: h.Args, + Env: h.Env, + } + config.Hooks.Poststart = append(config.Hooks.Poststart, configs.NewCommandHook(cmd)) + } for _, h := range rspec.Hooks.Poststop { cmd := configs.Command{ Path: h.Path,