Skip to content

Commit 8db1a3b

Browse files
committed
chore: Refactor Dockerfile for gem installation and precompilation
1 parent eb70a45 commit 8db1a3b

File tree

1 file changed

+21
-53
lines changed

1 file changed

+21
-53
lines changed

Dockerfile

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,30 @@
1-
# syntax = docker/dockerfile:1
1+
# Use the official Ruby 3.0.0 image as the base image
2+
FROM ruby:3.3.0
23

3-
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4-
ARG RUBY_VERSION=3.3.0
5-
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
6-
7-
# Rails app lives here
8-
WORKDIR /rails
9-
10-
# Set production environment
11-
ENV RAILS_ENV="development" \
12-
RAILS_LOG_TO_STDOUT="true" \
13-
RAILS_SERVE_STATIC_FILES="true" \
14-
RAILS_MAX_THREADS="5" \
15-
RAILS_MASTER_KEY=""
16-
17-
# Throw-away build stage to reduce size of final image
18-
FROM base as build
19-
20-
# Install packages needed to build gems
4+
# Install essential Linux packages
215
RUN apt-get update -qq && \
22-
apt-get install --no-install-recommends -y build-essential git libvips pkg-config cmake
23-
24-
# Install application gems
25-
COPY Gemfile Gemfile.lock ./
26-
RUN bundle install && \
27-
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
28-
bundle exec bootsnap precompile --gemfile
29-
30-
# Copy application code
31-
COPY . .
6+
apt-get install -y nodejs npm cmake && \
7+
npm install -g yarn && \
8+
apt-get clean && \
9+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
3210

33-
# Precompile bootsnap code for faster boot times
34-
RUN bundle exec bootsnap precompile app/ lib/
11+
# Set the working directory in the container
12+
WORKDIR /app
3513

36-
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
37-
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
14+
# Install Rails gem with version 7.1
15+
RUN gem install rails -v '7.1'
3816

17+
# Copy the Gemfile and Gemfile.lock into the container
18+
COPY Gemfile Gemfile.lock ./
3919

40-
# Final stage for app image
41-
FROM base
42-
43-
# Install packages needed for deployment
44-
RUN apt-get update -qq && \
45-
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
46-
rm -rf /var/lib/apt/lists /var/cache/apt/archives
47-
48-
# Copy built artifacts: gems, application
49-
COPY --from=build /usr/local/bundle /usr/local/bundle
50-
COPY --from=build /rails /rails
51-
52-
# Run and own only the runtime files as a non-root user for security
53-
RUN useradd rails --create-home --shell /bin/bash && \
54-
chown -R rails:rails db log storage tmp
55-
USER rails:rails
20+
# Install project dependencies
21+
RUN bundle install
5622

57-
# Entrypoint prepares the database.
58-
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
23+
# Copy the rest of the application code into the container
24+
COPY . .
5925

60-
# Start the server by default, this can be overwritten at runtime
26+
# Expose port 3000 to the Docker host
6127
EXPOSE 3000
62-
CMD ["./bin/rails", "server"]
28+
29+
# Start the Rails server
30+
CMD ["rails", "server", "-b", "0.0.0.0"]

0 commit comments

Comments
 (0)