Skip to content

Instantly share code, notes, and snippets.

@LEstradioto
Created July 3, 2024 19:54
Show Gist options
  • Select an option

  • Save LEstradioto/bf930db4909f46e753f8e02068614e41 to your computer and use it in GitHub Desktop.

Select an option

Save LEstradioto/bf930db4909f46e753f8e02068614e41 to your computer and use it in GitHub Desktop.
Docker Rails 7, Sidekiq, Two Redis Instances (cache and sidekiq) to Coolify
#!/bin/bash -e
# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
fi
exec "${@}"
version: "3.8"
services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
env_file:
- .env
ports:
- "3000:3000"
depends_on:
redis-rails:
condition: service_started
redis-sidekiq:
condition: service_started
postgres-db:
condition: service_healthy
postgres-db:
image: postgres
user: ${POSTGRES_USER}
env_file:
- .env
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: pg_isready
interval: 2s
timeout: 5s
retries: 30
redis-rails:
image: redis
command: redis-server --maxmemory-policy allkeys-lfu --requirepass $REDIS_PASSWORD
env_file:
- .env
volumes:
- redis-rails-data:/data
redis-sidekiq:
image: redis
command: redis-server --maxmemory-policy noeviction --requirepass $REDIS_PASSWORD
env_file:
- .env
volumes:
- redis-sidekiq-data:/data
sidekiq:
build: .
command: bundle exec sidekiq
env_file:
- .env
depends_on:
redis-sidekiq:
condition: service_started
postgres-db:
condition: service_healthy
volumes:
postgres-data:
redis-rails-data:
redis-sidekiq-data:
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# ENVs are external .env
ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
RAILS_LOG_TO_STDOUT="true" \
RAILS_SERVE_STATIC_FILES="true" \
RACK_ENV="production" \
RAILS_ENV="production"
# Update gems and bundler
RUN gem update --system --no-document && \
gem install -N bundler
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3
# Install JavaScript dependencies
ARG NODE_VERSION=20.5.1
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
bundle exec bootsnap precompile --gemfile && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
# Install node modules
COPY --link .yarnrc package.json yarn.lock ./
COPY --link .yarn/releases/* .yarn/releases/
RUN yarn install --frozen-lockfile
# Copy application code
COPY --link . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl imagemagick libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security
ARG UID=1000 \
GID=1000
RUN groupadd -f -g $GID rails && \
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER rails:rails
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment