Logo

Rails 7.2 Devcontainer 功能尝鲜

avatar roger 22 Aug 2024

Rails 7.2 新增加 Devcontainer 支持

Rails 7.2 最近发布了,其中的一个新功能是增加了对 Devcontainer 的支持。Devcontainer是一个能够简化开发环境配置的工具,它可以通过定义一个包含所有必要依赖的配置文件来快速搭建开发环境,并与Docker容器进行整合。

我关注这个功能是因为最近系统升级了一个小版本,导致用 brew 安装的 postgresql 启动不成功(报 icu4c 相关的 libicui18n.62.dylib 找不到),估计是用的 postgresql 版本过旧导致的,最后只能重新安装一个新版本的 postgresql。所以我开始关注 Devcontainer 这个功能,看看能不能用了它之后可以避免这种问题。

什么是 Devcontainer

Devcontainer 是一种用于配置和管理开发环境的工具,通常与容器技术结合使用。它可以让开发人员在不同的项目中使用相同的开发环境,在不同的操作系统和平台上进行开发,从而提高开发效率和一致性。

Devcontainer 支持的最大优点之一是它可以帮助开发者避免常见的 “在我的机器上可以正常工作” 问题。通过在容器中定义开发环境的配置,开发者只需拉取项目并运行 Devcontainer,就可以在几分钟内获得与团队其他成员相同的开发环境,无需担心依赖或配置的不一致性。

另一个 Devcontainer 的优点是它可以让开发者专注于编写代码,而不是花费时间在配置开发环境上。只需配置一个 Devcontainer 文件,在其中指定所需的环境和依赖,然后运行 Devcontainer,开发者就可以立即开始编写代码,而无需花费时间在安装和配置开发环境上。

然而,Devcontainer 也存在一些缺点。其中一个缺点是在某些情况下,配置一个复杂的开发环境可能会比较困难。特别是对于一些特殊的依赖或设置,可能需要更多的配置和调试来确保开发环境的稳定和正确性。

另一个缺点是使用 Devcontainer 可能会引入一定的学习曲线。开发者需要了解如何使用容器技术,如 Docker,并且需要了解如何配置一个 Devcontainer 文件。对于一些不熟悉容器技术的开发者来说,可能需要一定的时间来适应和学习。

但是这两个缺点对于 Rails 7.2 这个版本已经不存在了,因为我们可以直接使用官方的配置文件,不需要自己配置。

如何在 Rails 7.2 中使用 Devcontainer

Devcontainer 在 Rails 7.2 版本里面是一个可选的功能,如果你想使用它,可以在初始化项目的时候增加 --devcontainer 参数,例如:

rails new my_app --devcontainer

或者初始化之后执行下面的命令:

bin/rails devcontainer

这样就会在项目的根目录下生成一个 .devcontainer 目录,里面包含三个文件。

devcontainer.json 配置文件

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ruby
{
  "name": "my_app",
  "dockerComposeFile": "compose.yaml",
  "service": "rails-app",
  "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

  // Features to add to the dev container. More info: https://containers.dev/features.
  "features": {
    "ghcr.io/devcontainers/features/github-cli:1": {},
    "ghcr.io/rails/devcontainer/features/activestorage": {},
    "ghcr.io/rails/devcontainer/features/postgres-client": {}
  },

  "containerEnv": {
    "REDIS_URL": "redis://redis:6379/1",
    "DB_HOST": "postgres"
  },

  // Use 'forwardPorts' to make a list of ports inside the container available locally.
  "forwardPorts": [3000, 5432, 6379],

  // Configure tool-specific properties.
  // "customizations": {},

  // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
  // "remoteUser": "root",


  // Use 'postCreateCommand' to run commands after the container is created.
  "postCreateCommand": "bin/setup"
}

compose.yaml 配置文件

name: "my_app"

services:
  rails-app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile

    volumes:
    - ../..:/workspaces:cached

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

    # Uncomment the next line to use a non-root user for all processes.
    # user: vscode

    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
    # (Adding the "ports" property to this file will not forward from a Codespace.)
    depends_on:
    - redis
    - postgres

  redis:
    image: redis:7.2
    restart: unless-stopped
    volumes:
    - redis-data:/data

  postgres:
    image: postgres:16.1
    restart: unless-stopped
    networks:
    - default
    volumes:
    - postgres-data:/var/lib/postgresql/data
    environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres

volumes:
  redis-data:
  postgres-data:

Dockerfile 配置文件

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.4
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION

运行 Devcontainer

在 VS Code 中安装 Dev Containers 插件之后,然后打开项目,点击底部的 Reopen in Container 按钮,就可以启动 Devcontainer 了。我们之后就可以在 VS Code 的终端里面正常地开发项目了。

总结

Rails 7.2 新增加了 Devcontainer 的支持,可以帮助开发者快速搭建开发环境,提高开发效率和一致性。大家可以尝试在 Rails 7.2 中使用 Devcontainer,体验一下它的便利性和功能。

参考

Tags
rails
devcontainer
Kontaktieren Sie uns