将添加的文件的权限更改为Docker卷 [英] Changing permissions of added file to a Docker volume

查看:180
本文介绍了将添加的文件的权限更改为Docker卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker最佳做法指南中,它指出:


强烈建议您将VOLUME用于图像的任何可变和/或用户可维修的部分。

You are strongly encouraged to use VOLUME for any mutable and/or user-serviceable parts of your image.

通过查看例如的源代码 cpuguy83 / nagios image 可以清楚地看到完成,因为从nagios到apache配置目录的所有内容都可以作为卷使用。

And by looking at the source code for e.g. the cpuguy83/nagios image this can clearly be seen done, as everything from nagios to apache config directories are made available as volumes.

然而,看同一个映像,apache服务(和nagios的cgi-scripts)作为默认情况下, nagios 用户。所以现在我在一个泡菜,因为我似乎无法想像如何添加我自己的配置文件为了例如定义更多的nagios监控主机。我试过:

However, looking at the same image the apache service (and cgi-scripts for nagios) are run as the nagios user by default. So now I'm in a pickle, as I can't seem to figure how to add my own config files in order to e.g. define more hosts for nagios monitoring. 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"]

我正常建立,并尝试使用 docker run -d -p 8000:80< image_hash> ,但是我收到以下错误:

I build as normal, and try to run it with docker run -d -p 8000:80 <image_hash>, however I get the following error:


错误:无法打开配置文件'/opt/nagios/etc/conf.d/my_custom_config.cfg'用于阅读:Permission denied

Error: Cannot open config file '/opt/nagios/etc/conf.d/my_custom_config.cfg' for reading: Permission denied

确实,权限在文件夹中看起来像(apache进程运行为 nagios ):

And sure enough, the permissions in the folder looks like (whist the apache process runs as nagios):

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

现在,这已经回答了(为什么没有在Dockerfile中工作),但是除了更改原始Dockerfile之外,还没有提出适当的解决方案。

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.

说实话,我认为这里有一些核心概念,我没有把握(因为我无法看到将配置目录声明为VOLUME,而不是运行除root以外的服务) - 所以提供了一个如上所述的Dockerfile(它遵循Docker最佳做法,通过添加多个卷)是解决方案/问题:

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:


  • 要将NAGIOS_USER / APACHE_RUN_USER更改为root,并以root身份运行所有内容?

  • 要删除Nagios的Docker文件中的VOLUME声明?

  • 其他方法?

您如何将上面的nagios dockerfile 扩展到您自己的配置文件?

How would you extend the nagios dockerfile above with your own config file?

推荐答案

由于您正在添加自己的my_custom_config.cfg文件e在构建时直接进入容器,只需更改主机上my_custom_config.cfg文件的权限,然后使用docker build构建您的映像。主机权限将被复制到容器映像中。

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. The host machine permissions are copied into the container image.

这篇关于将添加的文件的权限更改为Docker卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆