diff options
author | 荒野無燈 <ttys3@outlook.com> | 2020-03-10 12:28:03 +0800 |
---|---|---|
committer | 荒野無燈 <ttys3@outlook.com> | 2020-03-14 21:54:12 +0800 |
commit | 194723f314f505de3d39afb7fd769bc02293fd88 (patch) | |
tree | a7a4a9f6ddfb8ddaaa3527c31472c5c502da9389 /pkg/systemd/generate/systemdgen.go | |
parent | f378e82e2d57ee60e5b0f973eb1ea2ee3a760428 (diff) | |
download | podman-194723f314f505de3d39afb7fd769bc02293fd88.tar.gz podman-194723f314f505de3d39afb7fd769bc02293fd88.tar.bz2 podman-194723f314f505de3d39afb7fd769bc02293fd88.zip |
force run container detached if container CreateCommand missing the detach param
the podman generated systemd service file has `Type=forking` service,
so the command after `ExecStart=` should not run in front.
if someone created a container and has the detach(`-d`) param missing
like this
```
podman create --name ngxdemo -P nginxdemos/hello
```
and generate the file with `--new` param:
```
podman generate systemd --name --new ngxdemo
```
because `podman run xxx` has no `-d` param,
so the container is not run in background and nerver exit.
and systemd will fail to start the service:
```
sudo systemctl start container-ngxdemo.service
Job for container-ngxdemo.service failed because a timeout was exceeded.
See "systemctl status container-ngxdemo.service" and "journalctl -xe" for details.
```
Signed-off-by: 荒野無燈 <ttys3@outlook.com>
Diffstat (limited to 'pkg/systemd/generate/systemdgen.go')
-rw-r--r-- | pkg/systemd/generate/systemdgen.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/pkg/systemd/generate/systemdgen.go b/pkg/systemd/generate/systemdgen.go index 00ddc63f3..f2798819f 100644 --- a/pkg/systemd/generate/systemdgen.go +++ b/pkg/systemd/generate/systemdgen.go @@ -164,6 +164,26 @@ func CreateContainerSystemdUnit(info *ContainerInfo, opts Options) (string, erro "--cidfile", "%t/%n-cid", "--cgroups=no-conmon", } + + // Enforce detaching + // + // since we use systemd `Type=forking` service + // @see https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type= + // when we generated systemd service file with the --new param, + // `ExecStart` will have `/usr/bin/podman run ...` + // if `info.CreateCommand` has no `-d` or `--detach` param, + // podman will run the container in default attached mode, + // as a result, `systemd start` will wait the `podman run` command exit until failed with timeout error. + hasDetachParam := false + for _, p := range info.CreateCommand[index:] { + if p == "--detach" || p == "-d" { + hasDetachParam = true + } + } + if !hasDetachParam { + command = append(command, "-d") + } + command = append(command, info.CreateCommand[index:]...) info.RunCommand = strings.Join(command, " ") info.New = true |