Improve docker setup script (#7378)

* Add set -e to docker-setup script

* Add error handling for db setup stage

* Sometimes Elasticsearch takes time to boot up

* Also delete docker_data

* search:setup must come first

* Revert "Sometimes Elasticsearch takes time to boot up"

This reverts commit e975f6e5c3edad084d4f2c4d8718675bf5c8d07c.

Reason for revert:
Instead of using wait-on, we can use Docker’s health check instead

See review comment:
https://github.com/thepracticaldev/dev.to/pull/7378#discussion_r411839255

* Use wait-on without installing into package.json

* Add /usr/src/app/node_modules to volumes

Otherwise, webpacker will not run in docker-compose.

See: https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds
This commit is contained in:
Thai Pangsakulyanont 2020-04-25 05:45:27 +07:00 committed by GitHub
parent a7e664fc9c
commit 5d97ca86c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -1,10 +1,26 @@
#!/usr/bin/env sh
set -e
echo "== Building Docker image (this may take a while) =="
docker-compose build
echo "== Ensuring Elasticsearch data directory is owned by the correct uid =="
docker-compose run elasticsearch chown elasticsearch:elasticsearch /usr/share/elasticsearch/data
echo "== Setting up database =="
docker-compose run web rails db:setup db:migrate search:setup data_updates:run
on_db_setup_failed() {
echo "Database setup failed with exit code $1." >&2
echo "" >&2
echo "Check the Docker logs using:" >&2
echo " docker-compose logs" >&2
echo "" >&2
echo "Once you fixed the error, try running the script again." >&2
echo "To also delete all data, run this before retrying:" >&2
echo " docker-compose down -v && sudo rm -rf docker_data" >&2
exit $1
}
docker-compose run web sh -c 'yarn global add wait-on && wait-on -t 30000 http-get://elasticsearch:9200/ && rails search:setup db:setup db:migrate data_updates:run' || on_db_setup_failed $?
echo "== Starting app =="
docker-compose up

View file

@ -16,6 +16,7 @@ services:
REDIS_SESSIONS_URL: redis://redis:6379
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: bundle exec rails server -b 0.0.0.0 -p 3000
webpacker:
build: .