docker-compose yml running a script after up

69,371

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.

Share:
69,371
Confidence
Author by

Confidence

Passionate for the web, php, python, docker, clean code And Vanilla Ice scream with chocolate sprinkles.

Updated on March 26, 2020

Comments

  • Confidence
    Confidence over 4 years

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

  • chadn
    chadn over 6 years
    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 veithen.github.io/2014/11/16/sigterm-propagation.html
  • nessuno
    nessuno over 6 years
    Very good point. I'm going to update the answer adding your suggestion. Thank you!
  • komizo
    komizo over 3 years
    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?