在 Docker 中写入 /proc/1/fd/1 时权限被拒绝

Permission Denied as root when writing to /proc/1/fd/1 in Docker

我有一个容器 运行 cron -f,它阻止容器像通常那样输出到 stdout。我需要我的 cron 作业输出到 stdout for datadog 和 docker logs 才能看到它。

我从这个答案中得到了写入 proc 文件的想法:

有了这个,我的 cron 开始失败了。当我尝试在容器内手动写入文件时,例如

echo 'hi' > /proc/1/fd/1

我被告知:

bash: 1: Permission denied

ls -l的结果是:

root@my-container:/proc/1/fd# ls -l
ls: cannot read symbolic link '0': Permission denied
ls: cannot read symbolic link '1': Permission denied
ls: cannot read symbolic link '2': Permission denied
ls: cannot read symbolic link '3': Permission denied
total 0
lrwx------ 1 root root 64 Mar  8 23:46 0
l-wx------ 1 root root 64 Mar  8 23:46 1
l-wx------ 1 root root 64 Mar  8 23:46 2
lrwx------ 1 root root 64 Mar  8 23:46 3

类似的问题在操作员的评论中有解决方案,但他的文件不知何故归他的数据库用户所有,而不是根用户。这个想法对我不起作用。 https://www.reddit.com/r/docker/comments/imx8zr/permission_denied_when_write_to_proc1fd1/

root 的权限如何被拒绝?或者,是否有更好的方法让我的 cron 输出显示在 stdout?

解决此问题的方法是让 non-root 用户拥有该文件。这可以通过在切换用户后从 CMD 在启动脚本中调用 cron -f 来完成。

在我的 Dockerfile 的末尾

USER worker

在我的 docker-compose:

cron_service:
  CMD: ["./start.sh"]

在start.sh

cron -f

现在为 cron 进程创建的 proc 文件将归 worker 所有,cron 可以写入。