Skip to content

Commit 831fb59

Browse files
Ruby language DHI (#23691)
## Description Upgraded Ruby language guide to include DHI-based Dockerfile example. ## Reviews - [ ] Technical review - [x] Editorial review - [ ] Product review --------- Co-authored-by: Craig Osterhout <[email protected]>
1 parent 3869859 commit 831fb59

File tree

1 file changed

+109
-2
lines changed

1 file changed

+109
-2
lines changed

content/guides/ruby/containerize.md

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ If you have an existing Rails application, you will need to create the Docker as
2626

2727
## 1. Initialize Docker assets
2828

29-
Rails 7.1 generates multistage Dockerfile out of the box, below is an example of such file generated from a Rails template.
29+
Rails 7.1 and newer generates multistage Dockerfile out of the box. Following are two versions of such a file: one using Docker Hardened Images (DHI) and another using the official Docker image.
30+
31+
> [Docker Hardened Images (DHIs)](https://docs.docker.com/dhi/) are minimal, secure, and production-ready container base and application images maintained by Docker.
32+
33+
DHI images are recommended whenever it is possible for better security. They are designed to reduce vulnerabilities and simplify compliance.
3034

3135
> Multistage Dockerfiles help create smaller, more efficient images by separating build and runtime dependencies, ensuring only necessary components are included in the final image. Read more in the [Multi-stage builds guide](/get-started/docker-concepts/building-images/multi-stage-builds/).
3236
3337
Although the Dockerfile is generated automatically, understanding its purpose and functionality is important. Reviewing the following example is highly recommended.
3438

39+
{{< tabs >}}
40+
{{< tab name="Using Docker Hardened Images" >}}
3541

3642
```dockerfile {title=Dockerfile}
3743
# syntax=docker/dockerfile:1
@@ -44,7 +50,104 @@ Although the Dockerfile is generated automatically, understanding its purpose an
4450
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
4551

4652
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
47-
ARG RUBY_VERSION=3.3.6
53+
ARG RUBY_VERSION=3.4.7
54+
FROM <your-namespace>/dhi-ruby:$RUBY_VERSION-dev AS base
55+
56+
# Rails app lives here
57+
WORKDIR /rails
58+
59+
# Install base packages
60+
# Replace libpq-dev with sqlite3 if using SQLite, or libmysqlclient-dev if using MySQL
61+
RUN apt-get update -qq && \
62+
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libpq-dev && \
63+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
64+
65+
# Set production environment
66+
ENV RAILS_ENV="production" \
67+
BUNDLE_DEPLOYMENT="1" \
68+
BUNDLE_PATH="/usr/local/bundle" \
69+
BUNDLE_WITHOUT="development"
70+
71+
# Throw-away build stage to reduce size of final image
72+
FROM base AS build
73+
74+
# Install packages needed to build gems
75+
RUN apt-get update -qq && \
76+
apt-get install --no-install-recommends -y build-essential curl git pkg-config libyaml-dev && \
77+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
78+
79+
# Install JavaScript dependencies and Node.js for asset compilation
80+
#
81+
# Uncomment the following lines if you are using NodeJS need to compile assets
82+
#
83+
# ARG NODE_VERSION=18.12.0
84+
# ARG YARN_VERSION=1.22.19
85+
# ENV PATH=/usr/local/node/bin:$PATH
86+
# RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
87+
# /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
88+
# npm install -g yarn@$YARN_VERSION && \
89+
# npm install -g mjml && \
90+
# rm -rf /tmp/node-build-master
91+
92+
# Install application gems
93+
COPY Gemfile Gemfile.lock ./
94+
RUN bundle install && \
95+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
96+
bundle exec bootsnap precompile --gemfile
97+
98+
# Install node modules
99+
#
100+
# Uncomment the following lines if you are using NodeJS need to compile assets
101+
#
102+
# COPY package.json yarn.lock ./
103+
# RUN --mount=type=cache,id=yarn,target=/rails/.cache/yarn YARN_CACHE_FOLDER=/rails/.cache/yarn \
104+
# yarn install --frozen-lockfile
105+
106+
# Copy application code
107+
COPY . .
108+
109+
# Precompile bootsnap code for faster boot times
110+
RUN bundle exec bootsnap precompile app/ lib/
111+
112+
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
113+
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
114+
115+
# Final stage for app image
116+
FROM base
117+
118+
# Copy built artifacts: gems, application
119+
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
120+
COPY --from=build /rails /rails
121+
122+
# Run and own only the runtime files as a non-root user for security
123+
RUN groupadd --system --gid 1000 rails && \
124+
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
125+
chown -R rails:rails db log storage tmp
126+
USER 1000:1000
127+
128+
# Entrypoint prepares the database.
129+
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
130+
131+
# Start server via Thruster by default, this can be overwritten at runtime
132+
EXPOSE 80
133+
CMD ["./bin/thrust", "./bin/rails", "server"]
134+
```
135+
136+
{{< /tab >}}
137+
{{< tab name="Using the official Docker image" >}}
138+
139+
```dockerfile {title=Dockerfile}
140+
# syntax=docker/dockerfile:1
141+
# check=error=true
142+
143+
# This Dockerfile is designed for production, not development.
144+
# docker build -t app .
145+
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name app app
146+
147+
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
148+
149+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
150+
ARG RUBY_VERSION=3.4.7
48151
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
49152

50153
# Rails app lives here
@@ -127,6 +230,9 @@ EXPOSE 80
127230
CMD ["./bin/thrust", "./bin/rails", "server"]
128231
```
129232

233+
{{< /tab >}}
234+
{{< /tabs >}}
235+
130236
The Dockerfile above assumes you are using Thruster together with Puma as an application server. In case you are using any other server, you can replace the last three lines with the following:
131237

132238
```dockerfile
@@ -279,3 +385,4 @@ Related information:
279385
## Next steps
280386

281387
In the next section, you'll take a look at how to set up a CI/CD pipeline using GitHub Actions.
388+

0 commit comments

Comments
 (0)