Skip to content

Commit 02dc1ce

Browse files
[demo] Optimize the Dockerfile (#5)
1 parent 9463605 commit 02dc1ce

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

Dockerfile

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
FROM ruby:3.3-slim AS base
2-
USER root
32

43
# Configure users and groups
54
RUN groupadd -g 40054 alma && \
6-
useradd -r -s /sbin/nologin -M -u 40054 -g alma alma && \
7-
useradd -u 40061 bfs && \
5+
useradd -s /sbin/nologin -M -u 40054 -g alma alma && \
86
groupadd -g 40061 bfs && \
9-
usermod -u 40061 -g bfs -G alma -l bfs default && \
10-
find / -user 1001 -exec chown -h bfs {} \; || true && \
11-
mkdir -p /opt/app && \
12-
chown -R bfs:bfs /opt/app
13-
14-
# Get list of available packages
15-
RUN apt-get -y update -qq
16-
17-
COPY --chown=bfs . /opt/app
7+
useradd -u 40061 -g bfs -G alma -m bfs && \
8+
install -d -o bfs -g bfs -m 0700 /opt/app /home/bfs/.ssh
9+
10+
# Install packages common to dev/prod
11+
RUN apt-get -y update -qq && \
12+
gem install bundler --version 2.5.22
13+
14+
# Ignore the system's platform and only install native Ruby versions
15+
ENV BUNDLE_FORCE_RUBY_PLATFORM=true
16+
# Prevent automatic updates to the Gemfile.lock
17+
ENV BUNDLE_FROZEN=true
18+
# Install Gems to the container's system-wide location
19+
ENV BUNDLE_SYSTEM=true
20+
# Prepend BFS script to PATH so you don't have to prefix with /opt/app/bin.
21+
ENV PATH=/opt/app/bin:$PATH
1822

23+
WORKDIR /opt/app
1924
ENTRYPOINT ["/opt/app/bin/bfs"]
2025
CMD ["help"]
2126

2227
# ===============================================
2328
# Target: development
2429
# ===============================================
25-
2630
FROM base AS development
2731

28-
USER root
29-
3032
RUN apt-get -y --no-install-recommends install \
31-
build-essential \
32-
make
33-
34-
USER bfs
33+
build-essential \
34+
make
3535

36-
# Base image ships with an older version of bundler
37-
RUN gem install bundler --version 2.5.22
38-
39-
WORKDIR /opt/app
36+
# Install rubygems. This step is separated from copying the
37+
# rest of the codebase to maximize cache hits.
4038
COPY --chown=bfs Gemfile* .ruby-version ./
41-
RUN bundle config set force_ruby_platform true
42-
RUN bundle config set system 'true'
4339
RUN bundle install
4440

45-
# COPY --chown=bfs:bfs . .
41+
# Install the rest of the codebase.
42+
COPY --chown=bfs:bfs . .
4643

4744
# =================================
4845
# Target: production
4946
# =================================
5047
FROM base AS production
5148

52-
# Copy the built codebase from the dev stage
53-
# COPY --from=development --chown=bfs /opt/app /opt/app
54-
COPY --from=development --chown=bfs /usr/local/bundle /usr/local/bundle
49+
# Copy the built codebase/dependencies from the dev stage
50+
COPY --from=development --chown=bfs:bfs /opt/app /opt/app
51+
COPY --from=development --chown=bfs:bfs /usr/local/bundle /usr/local/bundle
5552

56-
WORKDIR /opt/app
57-
RUN bundle config set frozen 'true'
58-
RUN bundle install --local
53+
# Drop privileges
54+
USER bfs
55+
56+
# Verify the installation
57+
RUN bundle check && \
58+
bfs help

0 commit comments

Comments
 (0)