From 3312ea794f2f65774eafd4527704654336f249f7 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 9 Jan 2025 14:44:16 +0100 Subject: [PATCH] Add separate entrypoints for web and sidekiq --- docker/Dockerfile | 25 ++++++++++---- docker/docker-compose.production.yml | 4 +-- docker/docker-compose.yml | 4 +-- docker/sidekiq-entrypoint.sh | 34 +++++++++++++++++++ ...docker-entrypoint.sh => web-entrypoint.sh} | 29 +++++++--------- 5 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 docker/sidekiq-entrypoint.sh rename docker/{docker-entrypoint.sh => web-entrypoint.sh} (63%) diff --git a/docker/Dockerfile b/docker/Dockerfile index 330334bf..ce4d8455 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -3,7 +3,6 @@ FROM ruby:3.3.4-alpine ENV APP_PATH=/var/app ENV BUNDLE_VERSION=2.5.21 ENV BUNDLE_PATH=/usr/local/bundle/gems -ENV TMP_PATH=/tmp/ ENV RAILS_LOG_TO_STDOUT=true ENV RAILS_PORT=3000 @@ -22,10 +21,11 @@ RUN apk -U add --no-cache \ less \ yaml-dev \ gcompat \ - && rm -rf /var/cache/apk/* \ && mkdir -p $APP_PATH -RUN gem install bundler --version "$BUNDLE_VERSION" \ +# Update gem system and install bundler +RUN gem update --system 3.6.2 \ + && gem install bundler --version "$BUNDLE_VERSION" \ && rm -rf $GEM_HOME/cache/* # Navigate to app directory @@ -35,13 +35,26 @@ COPY ../Gemfile ../Gemfile.lock ../vendor ../.ruby-version ./ # Install missing gems RUN bundle config set --local path 'vendor/bundle' \ - && bundle install --jobs 20 --retry 5 + && if [ "$RAILS_ENV" = "production" ]; then \ + bundle install --jobs 4 --retry 3 --without development test; \ + else \ + bundle install --jobs 4 --retry 3; \ + fi COPY ../. ./ +# Precompile assets for production +RUN if [ "$RAILS_ENV" = "production" ]; then \ + bundle exec rake assets:precompile \ + && rm -rf node_modules tmp/cache; \ + fi + # Copy entrypoint scripts and grant execution permissions -COPY ./docker/docker-entrypoint.sh /usr/local/bin/entrypoint.sh -RUN chmod +x /usr/local/bin/entrypoint.sh +COPY ./docker/web-entrypoint.sh /usr/local/bin/web-entrypoint.sh +RUN chmod +x /usr/local/bin/web-entrypoint.sh + +COPY ./docker/sidekiq-entrypoint.sh /usr/local/bin/sidekiq-entrypoint.sh +RUN chmod +x /usr/local/bin/sidekiq-entrypoint.sh EXPOSE $RAILS_PORT diff --git a/docker/docker-compose.production.yml b/docker/docker-compose.production.yml index ce5ba6db..3603eefb 100644 --- a/docker/docker-compose.production.yml +++ b/docker/docker-compose.production.yml @@ -49,7 +49,7 @@ services: # - 9394:9394 # Prometheus exporter, uncomment if needed stdin_open: true tty: true - entrypoint: entrypoint.sh + entrypoint: web-entrypoint.sh command: ['bin/rails', 'server', '-p', '3000', '-b', '::'] restart: on-failure environment: @@ -106,7 +106,7 @@ services: - dawarich stdin_open: true tty: true - entrypoint: entrypoint.sh + entrypoint: sidekiq-entrypoint.sh command: ['bundle', 'exec', 'sidekiq'] restart: on-failure environment: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index ea7f5789..bee3df25 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -51,7 +51,7 @@ services: # - 9394:9394 # Prometheus exporter, uncomment if needed stdin_open: true tty: true - entrypoint: entrypoint.sh + entrypoint: web-entrypoint.sh command: ['bin/rails', 'server', '-p', '3000', '-b', '::'] restart: on-failure environment: @@ -104,7 +104,7 @@ services: - dawarich stdin_open: true tty: true - entrypoint: entrypoint.sh + entrypoint: sidekiq-entrypoint.sh command: ['sidekiq'] restart: on-failure environment: diff --git a/docker/sidekiq-entrypoint.sh b/docker/sidekiq-entrypoint.sh new file mode 100644 index 00000000..1083891b --- /dev/null +++ b/docker/sidekiq-entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +unset BUNDLE_PATH +unset BUNDLE_BIN + +set -e + +echo "⚠️ Starting Sidekiq in $RAILS_ENV environment ⚠️" + +# Parse DATABASE_URL if present, otherwise use individual variables +if [ -n "$DATABASE_URL" ]; then + # Extract components from DATABASE_URL + DATABASE_HOST=$(echo $DATABASE_URL | awk -F[@/] '{print $4}') + DATABASE_PORT=$(echo $DATABASE_URL | awk -F[@/:] '{print $5}') + DATABASE_USERNAME=$(echo $DATABASE_URL | awk -F[:/@] '{print $4}') + DATABASE_PASSWORD=$(echo $DATABASE_URL | awk -F[:/@] '{print $5}') +else + # Use existing environment variables + DATABASE_HOST=${DATABASE_HOST} + DATABASE_PORT=${DATABASE_PORT} + DATABASE_USERNAME=${DATABASE_USERNAME} + DATABASE_PASSWORD=${DATABASE_PASSWORD} +fi + +# Wait for the database to become available +echo "⏳ Waiting for database to be ready..." +until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -c '\q'; do + >&2 echo "Postgres is unavailable - retrying..." + sleep 2 +done +echo "✅ PostgreSQL is ready!" + +# run sidekiq +bundle exec sidekiq diff --git a/docker/docker-entrypoint.sh b/docker/web-entrypoint.sh similarity index 63% rename from docker/docker-entrypoint.sh rename to docker/web-entrypoint.sh index 1fe36928..cfd52116 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/web-entrypoint.sh @@ -5,7 +5,7 @@ unset BUNDLE_BIN set -e -echo "⚠️ Environment: $RAILS_ENV ⚠️" +echo "⚠️ Starting Rails environment: $RAILS_ENV ⚠️" # Parse DATABASE_URL if present, otherwise use individual variables if [ -n "$DATABASE_URL" ]; then @@ -27,14 +27,16 @@ fi # Remove pre-existing puma/passenger server.pid rm -f $APP_PATH/tmp/pids/server.pid -# Install gems -gem update --system 3.6.2 -gem install bundler --version '2.5.21' +# Wait for the database to become available +echo "⏳ Waiting for database to be ready..." +until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -c '\q'; do + >&2 echo "Postgres is unavailable - retrying..." + sleep 2 +done +echo "✅ PostgreSQL is ready!" -# Create the database if it doesn't exist -if PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -c "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" | grep -q 1; then - echo "Database $DATABASE_NAME already exists, skipping creation..." -else +# Create database if it doesn't exist +if ! PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -c "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" | grep -q 1; then echo "Creating database $DATABASE_NAME..." bundle exec rails db:create fi @@ -47,14 +49,9 @@ bundle exec rails db:migrate echo "Running DATA migrations..." bundle exec rake data:migrate -# Run seeds -echo "Running seeds..." -bundle exec rake db:seed - -# Precompile assets -if [ "$RAILS_ENV" = "production" ]; then - echo "Precompiling assets..." - bundle exec rake assets:precompile +if [ "$RAILS_ENV" != "production" ]; then + echo "Running seeds..." + bundle exec rails db:seed fi # run passed commands