43

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

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.

#!/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).

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!)

Viacheslav Shalamov
  • 4,149
  • 6
  • 44
  • 66
Confidence
  • 2,283
  • 3
  • 33
  • 57
  • 1
    Your trying to make things run automatically after up or is manually ok? you can use `docker exec $(docker-compose ps -q web) some_command` to run things in an already running container. – Mattias Wadman Oct 08 '15 at 08:29

1 Answers1

42

command overrides the default command.

That's the reason your container stops: nginx never starts.

At the end of your script you have to run 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.

Note that by doing so, NginX will never catch the SIGTERM signal sent by docker stop. 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. This replaces the bash process with nginx itself.

nessuno
  • 26,493
  • 5
  • 83
  • 74
  • 4
    You'll want to change that last line to `exec nginx`, which means your bash script will be replaced by nginx, instead of spawning a new process. This is needed so docker can pass signals like SIGTERM to nginx. More at http://veithen.github.io/2014/11/16/sigterm-propagation.html – chadn Sep 28 '17 at 15:03
  • Very good point. I'm going to update the answer adding your suggestion. Thank you! – nessuno Sep 28 '17 at 16:55
  • What if script sequence needs to be turned over, I meas first run default command and then trigger some postscript. I tried such approach with similar case: I triggered arangod as first and once it triggered as a service my part within script would never be reached, since service is running. Any ideas? – komizo Nov 06 '20 at 17:33