Docker run script in host on docker-compose up

99封情书 提交于 2019-12-01 16:39:49
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

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.

  1. The host should run a small server (choose a port and specify a request type that you should be waiting for)
  2. The container, after it starts, should send this request to that server
  3. 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.

The script needs to run continuously in the foreground.

In your Dockerfile use the CMD directive and define the script as the parameter.

When using the cli, use docker run -d IMAGE SCRIPT

You can create an alias for docker-compose up. Put something like this in ~/.bash_aliases (in 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!