Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Created December 10, 2024 04:45
Show Gist options
  • Save iMagesh/253b9f70b8941728d548c99098238449 to your computer and use it in GitHub Desktop.
Save iMagesh/253b9f70b8941728d548c99098238449 to your computer and use it in GitHub Desktop.
dockerfile for aws fargate
# Multi-stage build for optimization
FROM ruby:3.2.2-slim AS builder
# Install essential build dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy only dependency files first for caching
COPY Gemfile Gemfile.lock ./
# Install gems with caching
RUN bundle config set --local without 'development test' \
&& bundle install --jobs 4 --retry 3
# Copy the rest of the application
COPY . .
# Precompile assets
RUN SECRET_KEY_BASE=placeholder bundle exec rails assets:precompile
# Final stage
FROM ruby:3.2.2-slim
# Install runtime dependencies
RUN apt-get update && apt-get install -y \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy from builder stage
COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /app /app
# Set production environment
ENV RAILS_ENV=production
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
# Expose port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Use non-root user for security
RUN addgroup --system rails && \
adduser --system --ingroup rails rails
USER rails
# Startup command with performance tuning
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000", "-e", "production"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment