bash Docker 在 docker-compose up 上的主机中运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43208979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker run script in host on docker-compose up
提问by Ilhicas
thank you for watching this question.
感谢您观看这个问题。
So my question relates to best practices on how to run a script on a docker-compose up directive.
所以我的问题与如何在 docker-compose up 指令上运行脚本的最佳实践有关。
Currently i'm sharing a volume between host and container to allow for the script changes to be visible to both host and container. Similar to a watching script polling for changes on configuration file.
目前我正在主机和容器之间共享一个卷,以允许主机和容器都可以看到脚本更改。类似于监视脚本轮询配置文件的更改。
The script has to act on host on changes according to predefined rules.
脚本必须根据预定义的规则对主机进行更改。
So my question is:
所以我的问题是:
How could I start this script on a docker-compose up directive or even from the dockerfile of the service, so that whenever the container goes up the "watcher" can find any changes being made and writing to.
我怎么能在 docker-compose up 指令上启动这个脚本,甚至从服务的 dockerfile 启动这个脚本,这样每当容器上升时,“观察者”就可以找到正在做的任何更改和写入。
The container in question will always run over a debian / ubuntu OS and should be architecture independent, meaning it should be able to run on ARM as well.
有问题的容器将始终在 debian / ubuntu 操作系统上运行,并且应该与架构无关,这意味着它也应该能够在 ARM 上运行。
Thanks in advance
提前致谢
I would like to know as well, why was the question downvoted just because some people don't understand it.
我也想知道,为什么这个问题只是因为有些人不理解而被否决。
I wish to run a script on the Host, not inside the container.. I need the Host to change its network interface configurations to easily adapt any environment The HOST needs to change I repeat.. This should be seamless to the user, and easily editable on a Web interface running Inside a CONTAINER to adapt to new environments.
我希望在主机上运行脚本,而不是在容器内.. 我需要主机更改其网络接口配置以轻松适应任何环境 主机需要更改我重复.. 这对用户来说应该是无缝的,并且很容易可在运行在容器内的 Web 界面上进行编辑以适应新环境。
I currently do this with a script running on the host based on crontab. I just wish to know the best practices and examples of how to run a script on HOST from INSIDE a CONTAINER, so that the deploy can be as easy for the installing operator to just run docker-compose up.
我目前使用基于 crontab 在主机上运行的脚本来执行此操作。我只想知道如何从容器内部在主机上运行脚本的最佳实践和示例,以便安装操作员只需运行 docker-compose up 就可以轻松部署。
Thank you
谢谢
采纳答案by tgogos
I just wish to know the best practices and examples of how to run a script on HOST from INSIDE a CONTAINER, so that the deploy can be as easy for the installing operator to just run docker-compose up
我只想知道如何从容器内部在主机上运行脚本的最佳实践和示例,以便安装操作员只需运行 docker-compose up 即可轻松部署
It seems that there is no best practice that can be applied to your case. A workaround proposed here: How to run shell script on host from docker container?is to use a client/server
trick.
似乎没有可应用于您的案例的最佳实践。这里提出了一种解决方法:How to run shell script on host from docker container? 就是用一client/server
招。
- The host should run a small server (choose a port and specify a request type that you should be waiting for)
- The container, after it starts, should send this request to that server
- The host should then run the script / trigger the changes you want
- 主机应该运行一个小型服务器(选择一个端口并指定您应该等待的请求类型)
- 容器启动后,应将此请求发送到该服务器
- 然后主机应该运行脚本/触发您想要的更改
This is something that might have serious security issues, so use at your own risk.
这可能有严重的安全问题,因此使用风险自负。
回答by Creek
回答by charli
You can create an alias for docker-compose up
. Put something like this in ~/.bash_aliases
(in Ubuntu):
您可以为docker-compose up
. 把这样的东西放在~/.bash_aliases
(在 Ubuntu 中):
alias up="docker-compose up; ~/your_script.sh"
I'm not sure if running scripts on the host from a container is possible, but if it's possible, it's a severe security flaw. Containers should be isolated, that's the point of using containers.
我不确定是否可以从容器在主机上运行脚本,但如果可能,这是一个严重的安全漏洞。容器应该是隔离的,这就是使用容器的意义所在。