|
1 | | -#FROM debian:bullseye-slim |
2 | | -FROM ruby:3.0 |
3 | | -USER root |
| 1 | +# ============================================================================= |
| 2 | +# Target: base |
| 3 | +# |
| 4 | +# The base stage scaffolds elements which are common to building and running |
| 5 | +# the application, such as installing ca-certificates, creating the app user, |
| 6 | +# and installing runtime system dependencies. |
| 7 | +FROM ruby:3.0.3-slim AS base |
| 8 | + |
| 9 | +# ------------------------------------------------------------ |
| 10 | +# Create the application user/group and installation directory |
| 11 | + |
| 12 | +# UCBEARS uses the "altmedia" user and group because (historical/permissions) reasons |
| 13 | +ENV APP_USER=alma |
| 14 | +ENV APP_UID=40054 |
| 15 | + |
| 16 | +RUN groupadd --system --gid $APP_UID $APP_USER \ |
| 17 | + && useradd --home-dir /opt/app --system --uid $APP_UID --gid $APP_USER $APP_USER |
| 18 | + |
| 19 | +RUN mkdir -p /opt/app \ |
| 20 | + && chown -R $APP_USER:$APP_USER /opt/app /usr/local/bundle |
4 | 21 |
|
5 | | -RUN apt-get update |
| 22 | +# ------------------------------------------------------------ |
| 23 | +# Install packages common to dev and prod. |
6 | 24 |
|
7 | | -# Create the application user/group and application directory |
8 | | -RUN groupadd -g 40054 alma && \ |
9 | | - useradd -r -s /sbin/nologin -M -u 40054 -g alma alma && \ |
10 | | - mkdir -p /opt/app && \ |
11 | | - chown -R alma:alma /opt/app |
| 25 | +# Install standard packages from the Debian repository |
| 26 | +RUN apt-get update -qq |
12 | 27 |
|
13 | | -# Run everything else as the alma user |
| 28 | +# ------------------------------------------------------------ |
| 29 | +# Run configuration |
| 30 | + |
| 31 | +# All subsequent commands are executed relative to this directory. |
14 | 32 | WORKDIR /opt/app |
15 | 33 |
|
16 | | -COPY --chown=alma Gemfile* ./ |
17 | | -RUN bundle install --system |
18 | | -COPY --chown=alma . . |
| 34 | +# Run as the application user to minimize risk to the host. |
| 35 | +USER $APP_USER |
19 | 36 |
|
20 | | -USER alma |
| 37 | +# Uses the get_gobi script as the entrypoint, so any arguments passed to `docker run` |
| 38 | +# at invocation are passed directly to this script. |
21 | 39 | ENTRYPOINT ["ruby","/opt/app/lib/get_gobi.rb"] |
22 | | -#CMD ["help"] |
| 40 | + |
| 41 | +# ============================================================================= |
| 42 | +# Target: development |
| 43 | +# |
| 44 | +# The development stage installs build dependencies (system packages needed to |
| 45 | +# install all your gems) along with your bundle. It's "heavier" than the |
| 46 | +# production target. |
| 47 | +FROM base AS development |
| 48 | + |
| 49 | +# ------------------------------------------------------------ |
| 50 | +# Install build packages |
| 51 | + |
| 52 | +# Temporarily switch back to root |
| 53 | +USER root |
| 54 | + |
| 55 | +# Install system packages needed to build gems with C extensions. |
| 56 | +RUN apt-get install -y --no-install-recommends \ |
| 57 | + g++ \ |
| 58 | + git \ |
| 59 | + make |
| 60 | + |
| 61 | +# ------------------------------------------------------------ |
| 62 | +# Install Ruby gems |
| 63 | + |
| 64 | +# Drop back to $APP_USER. |
| 65 | +USER $APP_USER |
| 66 | + |
| 67 | +# Base image ships with an older version of bundler |
| 68 | +RUN gem install bundler --version 2.2.33 |
| 69 | + |
| 70 | +# Install gems. We don't enforce the validity of the Gemfile.lock until the |
| 71 | +# final (production) stage. |
| 72 | +COPY --chown=$APP_USER:$APP_USER Gemfile* ./ |
| 73 | +RUN bundle install |
| 74 | + |
| 75 | +# Copy the rest of the codebase. We do this after bundle-install so that |
| 76 | +# changes unrelated to the gemset don't invalidate the cache and force a slow |
| 77 | +# re-install. |
| 78 | +COPY --chown=$APP_USER:$APP_USER . . |
| 79 | + |
| 80 | +# ============================================================================= |
| 81 | +# Target: production |
| 82 | +# |
| 83 | +# The production stage extends the base image with the application and gemset |
| 84 | +# built in the development stage. It includes runtime dependencies (including |
| 85 | +# test dependencies, due to quirks of our Jenkins build) but tries to minimize |
| 86 | +# heavyweight build dependencies. |
| 87 | +FROM base AS production |
| 88 | + |
| 89 | +# Copy the built codebase from the dev stage |
| 90 | +COPY --from=development --chown=$APP_USER /opt/app /opt/app |
| 91 | +COPY --from=development --chown=$APP_USER /usr/local/bundle /usr/local/bundle |
| 92 | + |
| 93 | +# Ensure the bundle is installed and the Gemfile.lock is synced. |
| 94 | +RUN bundle config set frozen 'true' |
| 95 | +RUN bundle install --local |
0 commit comments