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!)
The docker compose up command aggregates the output of each container (like docker compose logs --follow does). When the command exits, all containers are stopped. Running docker compose up --detach starts the containers in the background and leaves them running.
The key difference between docker run versus docker-compose is that docker run is entirely command line based, while docker-compose reads configuration data from a YAML file. The second major difference is that docker run can only start one container at a time, while docker-compose will configure and run multiple.
Dockerfile and docker-compose combined In this case, the docker-compose command actually uses a Dockerfile to build the latest version of a Docker image before it runs it. The docker-compose. yaml file can reference a Dockerfile and force a new Docker image to be built. However, the opposite is not true.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With