简体   繁体   English

更改添加文件对Docker卷的权限

[英]Changing permissions of added file to a Docker volume

In the Docker best practices guide it states: Docker最佳实践指南中,它指出:

You are strongly encouraged to use VOLUME for any mutable and/or user-serviceable parts of your image. 强烈建议您将VOLUME用于图像的任何可变和/或用户可维修的部分。

And by looking at the source code for eg the cpuguy83/nagios image this can clearly be seen done, as everything from nagios to apache config directories are made available as volumes. 通过查看例如cpuguy83 / nagios图像的源代码,可以清楚地看到这已完成,因为从nagios到apache config目录的所有内容都可以作为卷使用。

However, looking at the same image the apache service (and cgi-scripts for nagios) are run as the nagios user by default. 但是,默认情况下,查看同一张图片时,apache服务(以及nagios的cgi脚本)以nagios用户身份运行。 So now I'm in a pickle, as I can't seem to figure how to add my own config files in order to eg define more hosts for nagios monitoring. 所以现在我很烦,因为我似乎无法弄清楚如何添加自己的配置文件来例如定义更多主机以进行nagios监视。 I've tried: 我试过了:

FROM cpuguy83/nagios
ADD my_custom_config.cfg /opt/nagios/etc/conf.d/
RUN chown nagios: /opt/nagios/etc/conf.d/my_custom_config.cfg
CMD ["/opt/local/bin/start_nagios"]

I build as normal, and try to run it with docker run -d -p 8000:80 <image_hash> , however I get the following error: 我正常构建,并尝试使用docker run -d -p 8000:80 <image_hash>运行它,但是出现以下错误:

Error: Cannot open config file '/opt/nagios/etc/conf.d/my_custom_config.cfg' for reading: Permission denied 错误:无法打开配置文件“ /opt/nagios/etc/conf.d/my_custom_config.cfg”以进行读取:权限被拒绝

And sure enough, the permissions in the folder looks like (whist the apache process runs as nagios ): 确实,该文件夹中的权限如下所示(apache进程以nagios运行):

# ls -l /opt/nagios/etc/conf.d/
-rw-rw---- 1 root root 861 Jan  5 13:43 my_custom_config.cfg

Now, this has been answered before (why doesn't chown work in Dockerfile) , but no proper solution other than "change the original Dockerfile" has been proposed. 现在, 这已经得到了回答(为什么在Dockerfile中不起作用) ,但是除了“更改原始Dockerfile”之外,没有提出其他适当的解决方案。

To be honest, I think there's some core concept here I haven't grasped (as I can't see the point of declaring config directories as VOLUME nor running services as anything other than root) - so provided a Dockerfile as above (which follows Docker best practices by adding multiple volumes) is the solution/problem: 老实说,我认为这里还没有掌握一些核心概念(因为我看不到将配置目录声明为VOLUME或将服务运行为除root以外的其他服务)-因此,如上所述提供了一个Dockerfile(如下)解决方案/问题是通过添加多个卷来实现的Docker最佳实践)

  • To change NAGIOS_USER/APACHE_RUN_USER to 'root' and run everything as root? 要将NAGIOS_USER / APACHE_RUN_USER更改为“ root”并以root用户身份运行所有内容?
  • To remove the VOLUME declarations in the Dockerfile for nagios? 要删除Dockerfile中用于nagios的VOLUME声明?
  • Other approaches? 其他方法?

How would you extend the nagios dockerfile above with your own config file? 您如何使用自己的配置文件扩展上面nagios dockerfile

Since you are adding your own my_custom_config.cfg file directly into the container at build time just change the permissions of the my_custom_config.cfg file on your host machine and then build your image using docker build. 由于您是在构建时将自己的my_custom_config.cfg文件直接添加到容器中,因此只需更改主机上my_custom_config.cfg文件的权限,然后使用docker build即可构建映像。 The host machine permissions are copied into the container image. 主机权限将复制到容器映像中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM