简体   繁体   English

docker-compose yml在运行后运行脚本

[英]docker-compose yml running a script after up

I want to run a script, right after running 我想在运行后立即运行脚本

`docker-compose up -d` 

Here is my snippet of docker-compose.yml . 这是我docker-compose.yml片段。 The other settings are mysql server, redis...etc....but they are not causing any problems 其他设置是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. 因此,每当我运行docker-compose up -ddocker-compose up它都会停止。 (the containers do not keep running). (容器不能继续运行)。 Although my shell script is simple (running echos...or phpunit). 虽然我的shell脚本很简单(运行echos ...或phpunit)。 Here is my script. 这是我的剧本。

#!/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. 就像服务器(nginx)还没有运行一样。 Also if I connect to the server using exec bash, and i check out the processes. 此外,如果我使用exec bash连接到服务器,并检查进程。 I do not see nginx running (yet). 我没有看到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!) (我在做什么,设置错误!)

command overrides 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 installcomposer *update (因此只有当/ src / myweb中的某个文件不存在时),因为它会使你的容器启动时间徒然增加。

Note that by doing so, NginX will never catch the SIGTERM signal sent by docker stop. 请注意,通过这样做,NginX将永远不会捕获docker stop发送的SIGTERM信号。 That can cause it to be abruptly killed. 这可能会导致它突然被杀死。

Instead, if you want to be sure that SIGTERM is received by nginx, you have to replace the last line with exec nginx . 相反,如果你想确保nginx收到SIGTERM,你必须用exec nginx替换最后一行。 This replaces the bash process with nginx itself. 这用nginx本身取代了bash过程。

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

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