bash docker-compose yml 启动后运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33009825/
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-compose yml running a script after up
提问by Confidence
I want to run a script, right after running
我想运行一个脚本,就在运行后
`docker-compose up -d`
Here is my snippet of docker-compose.yml
. The other settings are mysql server, redis...etc....but they are not causing any problems
这是我的片段docker-compose.yml
。其他设置是mysql服务器,redis...等....但它们没有引起任何问题
web:
image: nginx
container_name: web-project
volumes:
- ./code:/srv
working_dir: /srv/myweb
extra_hosts:
- "myweb.local:127.0.0.1"
ports:
- 8081:80
# tty: true
command: sh /srv/scripts/post-run-web.sh
So whenever i run docker-compose up -d
or docker-compose up
It all stops. (the containers do not keep running). Although my shell script is simple (running echos...or phpunit). Here is my script.
所以每当我跑步 docker-compose up -d
或 docker-compose up
一切都停止时。(容器不会继续运行)。尽管我的 shell 脚本很简单(运行 echos...或 phpunit)。这是我的脚本。
#!/bin/bash
echo running post install scripts for web..;
cd /srv/myweb
npm install
composer self-update
composer update
And this is the error I get. It is like the server (nginx) is not running yet. Also if I connect to the server using exec bash, and i check out the processes. I do not see nginx running (yet).
这是我得到的错误。就像服务器(nginx)还没有运行。此外,如果我使用 exec bash 连接到服务器,并检查进程。我没有看到 nginx 正在运行(还)。
web_1 | You are already using composer version 7a9eb02190d334513e99a479510f87eed18cf958.
web_1 | Loading composer repositories with package information
web_1 | Updating dependencies (including require-dev)
web_1 | Generating autoload files
web-project exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Stopping mysql-project... done
Stopping rabbitmq-project... done
Stopping redis-project... done
So why is it exiting, although the script is syntax-wise correct? How can i make it run correctly? (what am I doing, setting up wrong!)
那么为什么它会退出,尽管脚本在语法上是正确的?我怎样才能让它正确运行?(我在做什么,设置错误!)
采纳答案by nessuno
commandoverrides the default command.
命令覆盖默认命令。
That's the reason your container stops: nginx never starts.
这就是您的容器停止的原因:nginx 永远不会启动。
At the end of your script you have to run nginx
在脚本结束时,您必须运行 nginx
#!/bin/bash
echo running post install scripts for web..;
cd /srv/myweb
npm install
composer self-update
composer update
nginx
By the way, I suggest you to change your script and run npm install
and composer *update
only if required (thus only if some file in /src/myweb does not exists), because it makes your container startup time increase in vain.
顺便说一句,我建议你改变你的脚本并运行npm install
,并composer *update
仅在必要时(因此只有在/ src目录/ MyWeb即可一些文件不存在),因为它使白白你的容器启动时间增加。
Note that by doing so, NginX will never catch the SIGTERM signal sent by docker stop. That can cause it to be abruptly killed.
请注意,这样做,NginX 将永远不会捕获 docker stop 发送的 SIGTERM 信号。这可能会导致它被突然杀死。
Instead, if you want to be sure that SIGTERM is received by nginx, you have to replace the last line with exec nginx
. This replaces the bash process with nginx itself.
相反,如果您想确保 nginx 收到 SIGTERM,则必须将最后一行替换为exec nginx
. 这用 nginx 本身替换了 bash 进程。