Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Commit 50f992a

Browse files
Add pid file support
1 parent e7eea7c commit 50f992a

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ The main magic of how this works is that the container processes are moved from
8383

8484
The above will use the `name=systemd` and `cpu` cgroups of systemd but then use Docker's cgroups for all the others, like the freezer cgroup.
8585

86+
Pid File
87+
--------
88+
89+
If for whatever reason you want to create a pid file for the container PID, you can. Just add `--pid-file` as below
90+
91+
`ExecStart=/opt/bin/systemd-docker --pid-file=/var/run/%n.pid --env run --rm --name %n nginx`
92+
8693
systemd-notify support
8794
----------------------
8895

main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Context struct {
4141
NotifySocket string
4242
Cmd *exec.Cmd
4343
Pid int
44+
PidFile string
4445
Client *dockerClient.Client
4546
}
4647

@@ -72,10 +73,11 @@ func parseContext(args []string) (*Context, error) {
7273
AllCgroups: false,
7374
}
7475

75-
flags := flag.NewFlagSet("run", flag.ContinueOnError)
76+
flags := flag.NewFlagSet("systemd-docker", flag.ContinueOnError)
7677

7778
var flCgroups opts.ListOpts
7879

80+
flags.StringVar(&c.PidFile, []string{"p", "-pid-file"}, "", "pipe file")
7981
flags.BoolVar(&c.Logs, []string{"l", "-logs"}, true, "pipe logs")
8082
flags.BoolVar(&c.Notify, []string{"n", "-notify"}, false, "setup systemd notify for container")
8183
flags.BoolVar(&c.Env, []string{"e", "-env"}, false, "inherit environment variable")
@@ -443,6 +445,19 @@ func notify(c *Context) error {
443445
return nil
444446
}
445447

448+
func pidFile(c *Context) error {
449+
if len(c.PidFile) == 0 || c.Pid <= 0 {
450+
return nil
451+
}
452+
453+
err := ioutil.WriteFile(c.PidFile, []byte(strconv.Itoa(c.Pid)), 0644)
454+
if err != nil {
455+
return err
456+
}
457+
458+
return nil
459+
}
460+
446461
func pipeLogs(c *Context) error {
447462
if !c.Logs {
448463
return nil
@@ -527,6 +542,11 @@ func mainWithArgs(args []string) (*Context, error) {
527542
return c, err
528543
}
529544

545+
err = pidFile(c)
546+
if err != nil {
547+
return c, err
548+
}
549+
530550
go pipeLogs(c)
531551

532552
err = keepAlive(c)

main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"log"
56
"os"
67
"os/exec"
78
"path"
9+
"strconv"
810
"syscall"
911
"testing"
1012

@@ -452,3 +454,40 @@ func TestNamedContainerAttach(t *testing.T) {
452454

453455
deleteTestContainer(t)
454456
}
457+
458+
func Exist(path string) bool {
459+
_, err := os.Stat(path)
460+
return os.IsExist(err)
461+
}
462+
463+
func TestPidFile(t *testing.T) {
464+
client, err := getClient(&Context{})
465+
if err != nil {
466+
t.Fatal(err)
467+
}
468+
469+
pidFileName := "./pid-file"
470+
471+
os.Remove(pidFileName)
472+
473+
c, err := mainWithArgs([]string{"--logs=false", "--pid-file", "./pid-file", "run", "--rm", "busybox", "echo", "hi"})
474+
if err != nil {
475+
t.Fatal(err)
476+
}
477+
478+
_, err = client.InspectContainer(c.Id)
479+
if err == nil {
480+
t.Fatal("Container should not exist")
481+
}
482+
483+
bytes, err := ioutil.ReadFile(pidFileName)
484+
if err != nil {
485+
t.Fatal(err)
486+
}
487+
488+
if string(bytes) != strconv.Itoa(c.Pid) {
489+
t.Fatal("Failed to write pid file")
490+
}
491+
492+
os.Remove(pidFileName)
493+
}

0 commit comments

Comments
 (0)