为什么我不能从主机的命令行在 docker 容器中执行 kill 命令?



这就完成了任务。

//get inside container
$ docker exec -it -u root container bash
// run kill ( works perfectly )
# kill -9 $(pgrep gunicorn)

但我想直接执行,而不必登录到容器的外壳。

我试试这个。

$ docker exec -it -u root container bash -c "kill -9 $(pgrep gunicorn)"

我得到这个错误。

bash: line 0: kill: (1227) - No such process
bash: line 1: 1988: command not found
bash: line 2: 8789: command not found
bash: line 3: 8989: command not found
bash: line 4: 11471: command not found
bash: line 5: 97065: command not found
bash: line 6: 729921: command not found
bash: line 7: 750108: command not found
bash: line 8: 851794: command not found
bash: line 9: 851862: command not found

我尝试另一种东西。

$ docker exec -it -u root container bash -c "touch test-file"

这也很好用。

那么,为什么我不能像这样运行kill命令呢?

根据@SiHa的建议,必须使用单引号来阻止主机上的shell对表达式求值。将双引号替换为单引号后工作良好。

最新更新