linux - 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.
解决方案
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.
推荐阅读
- css - 如何使用 simple_form_for 在 Rails 应用程序中更改文本区域的大小和位置?
- java - 无法将照片从相机上传到服务器。IllegalArgumentException:列“_data”不存在
- erlang - 从 Elixir 调用 :binary.match Erlang 模块但遇到选项问题
- angular - Angular 理解属性绑定
- azure - 构建失败,“npm”未被识别为内部或外部命令
- google-cloud-platform - 向谷歌大查询插入数据时出现 503 错误
- php - preg_replace 不适用于手机号码格式
- c++ - 在类中始终使用此指针是否有任何性能成本
- angular - 在 ng-packagr 构建中包含 json 文件
- php - PHP 500(内部服务器错误) - 遇到非数字值?