From ff57efab01137b7ae6dd4486fdb16bdc4abb0473 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 15 Mar 2024 23:31:06 +0100 Subject: [PATCH] Add Dockerfile and update docker-compose.yml to use the new Dockerfile --- Dockerfile | 53 +++++++++++++++++++++++++++++ Makefile | 5 +++ config/initializers/sidekiq.rb | 7 ++++ dev-docker-entrypoint.sh | 17 ++++++++-- docker-compose.yml | 61 ++++++++++++---------------------- 5 files changed, 100 insertions(+), 43 deletions(-) create mode 100644 Dockerfile create mode 100644 config/initializers/sidekiq.rb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..961f490c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +FROM ruby:3.2.3-alpine + +ENV APP_PATH /var/app +ENV BUNDLE_VERSION 2.3.3 +ENV BUNDLE_PATH /usr/local/bundle/gems +ENV TMP_PATH /tmp/ +ENV RAILS_LOG_TO_STDOUT true +ENV RAILS_PORT 3000 + +# Copy entrypoint scripts and grant execution permissions +COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh +COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh +RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh + +# Copy application files to workdir +COPY . $APP_PATH + +# Install dependencies for application +RUN apk -U add --no-cache \ + build-base \ + git \ + postgresql-dev \ + postgresql-client \ + libxml2-dev \ + libxslt-dev \ + nodejs \ + yarn \ + imagemagick \ + tzdata \ + less \ + yaml-dev \ + # gcompat for nokogiri on mac m1 + gcompat \ + && rm -rf /var/cache/apk/* \ + && mkdir -p $APP_PATH + +RUN gem install bundler --version "$BUNDLE_VERSION" \ + && rm -rf $GEM_HOME/cache/* + +# Navigate to app directory +WORKDIR $APP_PATH + +COPY Gemfile Gemfile.lock ./ + +# Install missing gems +RUN bundle config set --local path 'vendor/bundle' \ + && bundle install --jobs 20 --retry 5 + +COPY . ./ + +EXPOSE $RAILS_PORT + +ENTRYPOINT [ "bundle", "exec" ] diff --git a/Makefile b/Makefile index 0e44ff05..8e005264 100644 --- a/Makefile +++ b/Makefile @@ -65,3 +65,8 @@ tail_production_log: production_migrate: ssh dokku_frey 'dokku run dawarich bundle exec rails db:migrate' + +build_and_push: + docker build . -t dawarich --platform=linux/amd64 --no-cache + docker tag dawarich registry.chibi.rodeo/dawarich + docker push registry.chibi.rodeo/dawarich diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb new file mode 100644 index 00000000..b0618051 --- /dev/null +++ b/config/initializers/sidekiq.rb @@ -0,0 +1,7 @@ +Sidekiq.configure_server do |config| + config.redis = { url: ENV['REDIS_URL'] } +end + +Sidekiq.configure_client do |config| + config.redis = { url: ENV['REDIS_URL'] } +end diff --git a/dev-docker-entrypoint.sh b/dev-docker-entrypoint.sh index 8a1b3fff..71bbcf37 100644 --- a/dev-docker-entrypoint.sh +++ b/dev-docker-entrypoint.sh @@ -4,11 +4,22 @@ set -e echo "Environment: $RAILS_ENV" -# install missing gems -bundle check || bundle install --jobs 20 --retry 5 - # Remove pre-existing puma/passenger server.pid rm -f $APP_PATH/tmp/pids/server.pid +# Wait for the database to be ready +until nc -zv wardu_db 5432; do + echo "Waiting for PostgreSQL to be ready..." + sleep 1 +done + +# Create the database +echo "Creating database $DATABASE_NAME..." +bundle exec rails db:create + +# Run database migrations +echo "PostgreSQL is ready. Running database migrations..." +bundle exec rails db:prepare + # run passed commands bundle exec ${@} diff --git a/docker-compose.yml b/docker-compose.yml index 737a896c..d4005310 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,42 +1,33 @@ version: '3' networks: development: - test: -volumes: - db_data: - gem_cache: - shared_data: services: - dawarich_redis: - image: redis:4.0-alpine + wardu_redis: + image: redis:7.0-alpine command: redis-server networks: - development - - test volumes: - shared_data:/var/shared/redis - dawarich_db: + ports: + - 6379:6379 + wardu_db: image: postgres:14.2-alpine - container_name: dawarich_db + container_name: wardu_db volumes: - db_data:/var/lib/postgresql/data - shared_data:/var/shared networks: - development - - test environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: password ports: - 5099:5432 - dawarich_app: - build: - context: . - dockerfile: Dockerfile.dev - container_name: dawarich_app + wardu_app: + image: registry.chibi.rodeo/wardu:latest + container_name: wardu_app volumes: - - .:/var/app - - shared_data:/var/shared - gem_cache:/usr/local/bundle/gems networks: - development @@ -44,30 +35,20 @@ services: - 3000:3000 stdin_open: true tty: true - env_file: .env.development entrypoint: dev-entrypoint.sh command: ['bin/dev'] environment: RAILS_ENV: development + REDIS_URL: redis://wardu_redis:6379/0 + DATABASE_HOST: wardu_db + DATABASE_USERNAME: postgres + DATABASE_PASSWORD: password + DATABASE_NAME: wardu_development depends_on: - - dawarich_db - dawarich_test: - image: dawarich_dawarich_app - container_name: dawarich_test - volumes: - - .:/var/app - - shared_data:/var/shared - - gem_cache:/usr/local/bundle/gems - networks: - - test - ports: - - 3001:3000 - stdin_open: true - tty: true - env_file: .env.test - entrypoint: test-entrypoint.sh - command: ["rails", "-v"] - environment: - RAILS_ENV: test - depends_on: - - dawarich_db + - wardu_db + - wardu_redis + +volumes: + db_data: + gem_cache: + shared_data: