容器化 Ruby on Rails 应用程序
先决条件
- 您已安装最新版本的 Docker Desktop。
- 您已安装 Git 客户端。本节中的示例展示了 Git CLI,但您可以使用任何客户端。
概述
本节将引导您完成 Ruby on Rails 应用程序的容器化和运行。
从 Rails 7.1 开始,Docker 已原生支持。这意味着当您创建新的 Rails 应用程序时,系统将为您生成 `Dockerfile`、`.dockerignore` 和 `bin/docker-entrypoint` 文件。
如果您有一个现有的 Rails 应用程序,您将需要手动创建 Docker 资产。不幸的是,`docker init` 命令尚不支持 Rails。这意味着如果您正在使用 Rails,您将需要手动从以下示例中复制 Dockerfile 和其他相关配置。
1. 初始化 Docker 资产
Rails 7.1 原生生成多阶段 Dockerfile,下面是根据 Rails 模板生成的文件示例。
多阶段 Dockerfile 通过分离构建和运行时依赖项来帮助创建更小、更高效的镜像,确保最终镜像中只包含必要的组件。有关更多信息,请参阅多阶段构建指南。
尽管 Dockerfile 是自动生成的,但了解其目的和功能非常重要。强烈建议查阅以下示例。
# syntax=docker/dockerfile:1
# check=error=true
# This Dockerfile is designed for production, not development.
# docker build -t app .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name app app
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.cn/getting_started_with_devcontainer.html
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.6
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /rails
# Install base packages
# Replace libpq-dev with sqlite3 if using SQLite, or libmysqlclient-dev if using MySQL
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libpq-dev && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base AS build
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git pkg-config libyaml-dev && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install JavaScript dependencies and Node.js for asset compilation
#
# Uncomment the following lines if you are using NodeJS need to compile assets
#
# ARG NODE_VERSION=18.12.0
# ARG YARN_VERSION=1.22.19
# ENV PATH=/usr/local/node/bin:$PATH
# RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
# /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
# npm install -g yarn@$YARN_VERSION && \
# npm install -g mjml && \
# rm -rf /tmp/node-build-master
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Install node modules
#
# Uncomment the following lines if you are using NodeJS need to compile assets
#
# COPY package.json yarn.lock ./
# RUN --mount=type=cache,id=yarn,target=/rails/.cache/yarn YARN_CACHE_FOLDER=/rails/.cache/yarn \
# yarn install --frozen-lockfile
# Copy application code
COPY . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER 1000:1000
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80
CMD ["./bin/thrust", "./bin/rails", "server"]上面的 Dockerfile 假定您将 Thruster 与 Puma 一起用作应用程序服务器。如果您使用任何其他服务器,您可以将最后三行替换为以下内容
# Start the application server
EXPOSE 3000
CMD ["./bin/rails", "server"]此 Dockerfile 使用位于 `./bin/docker-entrypoint` 的脚本作为容器的入口点。此脚本准备数据库并运行应用程序服务器。以下是此类脚本的示例。
#!/bin/bash -e
# Enable jemalloc for reduced memory usage and latency.
if [ -z "${LD_PRELOAD+x}" ]; then
LD_PRELOAD=$(find /usr/lib -name libjemalloc.so.2 -print -quit)
export LD_PRELOAD
fi
# If running the rails server then create or migrate existing database
if [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then
./bin/rails db:prepare
fi
exec "${@}"除了上面两个文件之外,您还需要一个 `.`dockerignore 文件。此文件用于从构建上下文中排除文件和目录。下面是 `.`dockerignore 文件的示例。
# See https://docs.container.net.cn/engine/reference/builder/#dockerignore-file for more about ignoring files.
# Ignore git directory.
/.git/
/.gitignore
# Ignore bundler config.
/.bundle
# Ignore all environment files.
/.env*
# Ignore all default key files.
/config/master.key
/config/credentials/*.key
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/.keep
# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/.keep
# Ignore assets.
/node_modules/
/app/assets/builds/*
!/app/assets/builds/.keep
/public/assets
# Ignore CI service files.
/.github
# Ignore development files
/.devcontainer
# Ignore Docker-related files
/.dockerignore
/Dockerfile*您可能需要的最后一个可选文件是 `compose.yaml` 文件,Docker Compose 使用它来定义构成应用程序的服务。由于使用的是 SQLite 数据库,因此无需为数据库定义单独的服务。唯一需要的服务是 Rails 应用程序本身。
services:
web:
build: .
environment:
- RAILS_MASTER_KEY
ports:
- "3000:80"您的应用程序文件夹中现在应该有以下文件
.dockerignorecompose.yamlDockerfilebin/docker-entrypoint
要了解有关这些文件的更多信息,请参阅以下内容
2. 运行应用程序
要运行应用程序,请在应用程序目录中的终端中运行以下命令。
$ RAILS_MASTER_KEY=<master_key_value> docker compose up --build
打开浏览器并访问 https://:3000 查看应用程序。您应该会看到一个简单的 Ruby on Rails 应用程序。
在终端中,按 ctrl+c 停止应用程序。
3. 在后台运行应用程序
您可以通过添加 `-d` 选项使应用程序脱离终端运行。在 `docker-ruby-on-rails` 目录中,在终端中运行以下命令。
$ docker compose up --build -d
打开浏览器,在 https://:3000 查看应用程序。
您应该会看到一个简单的 Ruby on Rails 应用程序。
在终端中,运行以下命令以停止应用程序。
$ docker compose down
有关 Compose 命令的更多信息,请参阅Compose CLI 参考。
摘要
在本节中,您学习了如何使用 Docker 容器化并运行 Ruby 应用程序。
相关信息
后续步骤
在下一节中,您将了解如何使用 GitHub Actions 设置 CI/CD 管道。