Add separate entrypoints for web and sidekiq

This commit is contained in:
Eugene Burmakin 2025-01-09 14:44:16 +01:00
parent 4d25dbca21
commit 3312ea794f
5 changed files with 70 additions and 26 deletions

View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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

View file

@ -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