From 5d97ca86c5d65281daa19bbcf5e1cafecbdaff81 Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Sat, 25 Apr 2020 05:45:27 +0700 Subject: [PATCH] Improve docker setup script (#7378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- bin/docker-setup | 18 +++++++++++++++++- docker-compose.yml | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bin/docker-setup b/bin/docker-setup index 82c55293d..67e217ef9 100755 --- a/bin/docker-setup +++ b/bin/docker-setup @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index ed0835c1f..7d5d7de78 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: .