首页 > 解决方案 > Killing subprocess from inside a Docker container kills the entire container

问题描述

On my Windows machine, I started a Docker container from docker compose. My entrypoint is a Go filewatcher that runs a task of a taskmanager on every filechange. The executed task builds and runs the Go program.

But before I can build and run the program again after filechanges I have to kill the previous running version. But every time I kill the app process, the container is also gone.

The goal is to kill only the svc1 process with PID 74 in this example. I tried pkill -9 svc1 and kill $(pgrep svc1). But every time the parent processes are killed too.

The commandline output from inside the container:

root@bf073c39e6a2:/app/cmd/svc1# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  2.5  0.0 104812  2940 ?        Ssl  13:38   0:00 /go/bin/watcher
root        13  0.0  0.0 294316  7576 ?        Sl   13:38   0:00 /go/bin/task de
root        74  0.0  0.0 219284  4908 ?        Sl   13:38   0:00 /svc1
root        82  0.2  0.0  18184  3160 pts/0    Ss   13:38   0:00 /bin/bash
root        87  0.0  0.0  36632  2824 pts/0    R+   13:38   0:00 ps -aux
root@bf073c39e6a2:/app/cmd/svc1# ps -afx
  PID TTY      STAT   TIME COMMAND
   82 pts/0    Ss     0:00 /bin/bash
   88 pts/0    R+     0:00  \_ ps -afx
    1 ?        Ssl    0:01 /go/bin/watcher -cmd /go/bin/task dev -startcmd
   13 ?        Sl     0:00 /go/bin/task dev
   74 ?        Sl     0:00  \_ /svc1
root@bf073c39e6a2:/app/cmd/svc1# pkill -9 svc1
root@bf073c39e6a2:/app/cmd/svc1

Switching to the containerlog:

task: Failed to run task "dev": exit status 255
2019/08/16 14:20:21 exit status 1

"dev" is the name of the task in the taskmanger.

The Dockerfile:

FROM golang:stretch
RUN go get -u -v github.com/radovskyb/watcher/... \
    && go get -u -v github.com/go-task/task/cmd/task
WORKDIR /app
COPY ./Taskfile.yml ./Taskfile.yml
ENTRYPOINT ["/go/bin/watcher", "-cmd", "/go/bin/task dev", "-startcmd"]

I expect only the process with the target PID is killed and not the parent process that spawned it it.

标签: linuxdockerprocesskill

解决方案


You can use process manager like "supervisord" and configure it to re-execute your script or the command even if you killed it's process which will keep your container up and running.


推荐阅读