Skip to content

Commit c0ff24d

Browse files
Rails docks upgrade interation 1
1 parent 2742bba commit c0ff24d

File tree

1 file changed

+101
-8
lines changed

1 file changed

+101
-8
lines changed

content/guides/ruby/containerize.md

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,112 @@ aliases:
1818

1919
## Overview
2020

21-
This section walks you through containerizing and running a Ruby on Rails application.
21+
This section walks you through containerizing and running a [Ruby on Rails](https://rubyonrails.org/) application.
2222

23-
## Get the sample application
23+
Starting from Rails 7.1 [Docker is supported out of the box](https://guides.rubyonrails.org/7_1_release_notes.html#generate-dockerfiles-for-new-rails-applications). This means that you will get a `Dockerfile`, `.dockerignore` and `bin/docker-entrypoint` files generated for you when you create a new Rails application.
2424

25-
The sample application uses the popular [Ruby on Rails](https://rubyonrails.org/) framework.
25+
If you have an existing Rails application, you will need to create the Docker assets manually. Unfortunately `docker init` command does not yet support Rails. This means that if you are working with Rails, you'll need to copy Dockerfile and other related configurations manually from the examples below.
2626

27-
Clone the sample application to use with this guide. Open a terminal, change directory to a directory that you want to work in, and run the following command to clone the repository:
27+
## Initialize Docker assets
2828

29-
```console
30-
$ git clone https://github.com/falconcr/docker-ruby-on-rails.git
31-
```
29+
Rails 7.1 generates multistage Dockerfile out of the box, below is an example of such file generated from a Rails template.
3230

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

35128
Now that you have an application, you can create the necessary Docker assets to
36129
containerize your application. You can use Docker Desktop's built-in Docker Init

0 commit comments

Comments
 (0)