From 008188930b64eab04b529c5913346825634cf579 Mon Sep 17 00:00:00 2001 From: sashinde8770 Date: Thu, 21 Dec 2023 17:19:35 +0530 Subject: [PATCH 1/2] Docker_file added --- .env | 7 ++++ Dockerfile | 35 ++++++++++++++++++++ Dockerfile.multi | 68 ++++++++++++++++++++++++++++++++++++++ Gemfile | 2 +- config/credentials.yml.enc | 2 +- config/database.yml | 7 ++-- docker-compose.yml | 38 +++++++++++++++++++++ entrypoint.sh | 13 ++++++++ 8 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 Dockerfile.multi create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh diff --git a/.env b/.env new file mode 100644 index 00000000..bfb0b822 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +RAILS_ENV=production +POSTGRES_HOST=db +POSTGRES_DB=Budgy_production +POSTGRES_USER=Budgy +POSTGRES_PASSWORD=password123 +RAILS_MASTER_KEY=6af8d01f85d81f971129fc7691e1bd4a + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5aa319bf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM ruby:3.0.2 + +RUN apt-get update -qq && \ + apt-get install -y build-essential libvips bash bash-completion libffi-dev tzdata postgresql nodejs npm yarn && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man + +WORKDIR /budget-app +COPY Gemfile Gemfile.lock ./ +RUN bundle install +COPY . . + +# Set production environment +ENV RAILS_LOG_TO_STDOUT="1" \ + RAILS_SERVE_STATIC_FILES="true" \ + RAILS_ENV="production" \ + BUNDLE_WITHOUT="development" + +# Precompile bootsnap code for faster boot times +RUN bundle exec bootsnap precompile --gemfile app/ lib/ + +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY +RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile + +EXPOSE 3000 + +# ENTRYPOINT PREPARES THE DATABASE +COPY entrypoint.sh /usr/bin/ +RUN chmod +x /usr/bin/entrypoint.sh +ENTRYPOINT ["/usr/bin/entrypoint.sh"] + +# Start the Rails server +CMD ["rails", "server", "-b", "0.0.0.0"] + + diff --git a/Dockerfile.multi b/Dockerfile.multi new file mode 100644 index 00000000..801ab046 --- /dev/null +++ b/Dockerfile.multi @@ -0,0 +1,68 @@ +# Build stage +FROM ruby:3.0.2 AS build +WORKDIR /app + +# Install system dependencies and cleanup +RUN apt-get update -qq && \ + apt-get install -y build-essential libvips bash bash-completion libffi-dev tzdata postgresql nodejs npm yarn && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man + +# Copy Gemfile and Gemfile.lock to the build stage +COPY Gemfile Gemfile.lock ./ + +# Install gems (dependencies) for the application +RUN bundle install + +# Copy the rest of the application source code +COPY . . + +# Set production environment variables +ENV RAILS_LOG_TO_STDOUT="1" \ + RAILS_SERVE_STATIC_FILES="true" \ + RAILS_ENV="production" \ + BUNDLE_WITHOUT="development" + +# Precompile bootsnap code for faster boot times +RUN bundle exec bootsnap precompile --gemfile app/ lib/ + +# Build the application (you may customize this step) +RUN bundle exec rake assets:precompile + +# Runtime stage +FROM ruby:3.0.2-slim +WORKDIR /app + +# Install the PostgreSQL Client in the Runtime Stage +RUN apt-get update -qq && \ + apt-get install -y --no-install-recommends \ + libpq-dev && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man + +# Set production environment variables +ENV RAILS_LOG_TO_STDOUT="1" \ + RAILS_SERVE_STATIC_FILES="true" \ + RAILS_ENV="production" \ + BUNDLE_WITHOUT="development" + +# Copy installed dependiencies gem +COPY --from=build /usr/local/bundle /usr/local/bundle + +# Copy all of the application's assets to the production image (assets:precompile) +COPY --from=build app/public/assets /app/public/assets + +# Copy bootsnap code for faster boot times +COPY --from=build app/tmp/cache/bootsnap /lib/ + +COPY . . +EXPOSE 3000 + +COPY entrypoint.sh /usr/bin/ +RUN chmod +x /usr/bin/entrypoint.sh + +ENTRYPOINT ["/usr/bin/entrypoint.sh"] + +# Start your application (adjust the command as needed) +CMD ["rails", "server", "-b", "0.0.0.0"] + diff --git a/Gemfile b/Gemfile index 811cccd1..e75bce2f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '3.1.2' +ruby '3.0.2' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem 'rails', '~> 7.0.1' diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index dc031244..5f82bdc3 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -UkKuGgSd5mr6Fkc9OMiDA+BVvH1Hyl0LyFp15xDfbpJ0J+9i8N5714p/DU/u6uVA0KbZ5pyqyByYn3QLFRS+I9L3YGctvMMOSgF3uPnxa1x67uIhYHja99effFXDm/PqsvvlmtBbIOfzpglepefZtWffOPOAtXBhXFO3RTA2/rS60ZIU6fWCl2tUNwb/X6LbSA86R1H214rRd8xPb87Dm7IsdVre6+QD9SRa8YB9w6cdMcbTGs3hmw8h3/q/hXZd4Y5p43hJ5u3sGa1gSFF4TKuLGMFgQ1wgZiv7dTc2n3YQtbOeqPUoVxGgDnwlaclv9u0knBuYer+j0NixTLxmMkUSfsXAX184vIYr9cTe/oITIAL/f5yhVVpwqWjgm9QTP3orbCriSXSJ2i0issngOGPUzqysMu4YJY5o--6dkopLxTwTxcPVxA--Ohniv+we6M9EnlUQ/tPAYg== \ No newline at end of file +Hy/O9uc0AarR2n8OSCnbQW+iq/A033SM1oPIYeZWxakfQwUnYI0Cs+CR0QxwEsZYmskG3AK2jzO7KL5n8Q9ZKqG2fMYX0Y+L2y8tVnvlcj1tK12tm5rg2NzE3AFxSFD9sDZCYtwQd0Ichb8BS932ne5Pki0DIEUN2I8HhtreZO7n8gmq4kWyLkt2qGHvM+Gxxytbp8juPbQOXQYRPuiGpeAAW7D1waM5xA5bQbv7KkTT7CncBDs9TdkVzoDPcp1jxq/0ogXkcmnMyJJ7QM3t8979/nKcuYa3Q0yPSZJ2cI2lXtio2mAkhyUWsXf+8VHImto2V9+FdbGmmGNB0Rq0Fyy5NEi9Iwh93NmnEbDPN8xYtR/SqicWg74kWzw1MrQVzj1Td0fAt6GMVzKDLfnJLEZEA2htW6qFXsLu--8wqkHMEbic8SXV1v--TJFtcZa5TnXwbSheY804WQ== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index a485f244..d7728aa4 100644 --- a/config/database.yml +++ b/config/database.yml @@ -17,6 +17,9 @@ default: &default adapter: postgresql encoding: unicode + username: postgres + password: admin123 + # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> @@ -82,5 +85,5 @@ test: production: <<: *default database: Budgy_production - username: Budgy - password: <%= ENV["Budgy_DATABASE_PASSWORD"] %> + username: postgres + password: admin123 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..fdf8c598 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3' +services: + app: + build: + context: . + dockerfile: Dockerfile.multi + command: "rails server" + container_name: ruby-on-rails + environment: + - RAILS_ENV=${RAILS_ENV} + - POSTGRES_HOST=${POSTGRES_HOST} + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - RAILS_MASTER_KEY=${RAILS_MASTER_KEY} + volumes: + - app-storage:/budget-app/storage + depends_on: + - db + ports: + - "3000:3000" + db: + image: postgres:14.2-alpine + container_name: demo_postgres + volumes: + - postgres_data:/var/lib/postgresql/data + command: + "postgres -c 'max_connections=500'" + environment: + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + ports: + - "5432:5432" + +volumes: + postgres_data: {} + app-storage: {} diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..342da1ea --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,13 @@ + GNU nano 6.2 entrypoint.sh +#!/bin/bash +set -e + +# If running the rails server then create or migrate existing database +if [ "${*}" == "./bin/rails server" ]; then + ./bin/rails db:create + ./bin/rails db:prepare +fi + + +# Then exec the container's main process (what's set as CMD in the Dockerfile). +exec "$@" From cf96b523b9e56b10d540972a673837a33acf6f8c Mon Sep 17 00:00:00 2001 From: sashinde8770 Date: Tue, 26 Dec 2023 16:25:12 +0530 Subject: [PATCH 2/2] entrypoint edited --- docker-compose.yml | 2 +- entrypoint.sh | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) mode change 100644 => 100755 entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index fdf8c598..54a4eded 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,7 +31,7 @@ services: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} ports: - - "5432:5432" + - "5433:5432" volumes: postgres_data: {} diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index 342da1ea..b34ad758 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,13 +1,10 @@ - GNU nano 6.2 entrypoint.sh #!/bin/bash -set -e # If running the rails server then create or migrate existing database -if [ "${*}" == "./bin/rails server" ]; then +if [ "${*}" = "./bin/rails server" ]; then ./bin/rails db:create ./bin/rails db:prepare fi - # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@"