3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

rails7.2に実装される予定のdevcontainerを覗いてみる

Last updated at Posted at 2024-07-31

はじめに

最近、devcontainerを利用することが増えており、開発環境が汚れずに済むので非常に気に入っています。
こちらの記事によると、Rails 7.2からdevcontainerがデフォルトで組み込まれるらしいです。

どのような構成になるのか気になったので、調査結果をまとめました。

これはベータ版に基づいた調査です。実際にリリースされるバージョンとは若干の差異がある可能性があります。

環境構築

以前作った環境を利用します

作業内容

リリースを見ると、(7/30時点だと)「v7.2.0.beta3」が最新なので、こいつを利用します

Gemfile
- gem "rails", "~> 7.1.3", ">= 7.1.3.4"
+ gem "rails", github: "rails/rails", tag: "v7.2.0.beta3"
wslで実行コマンド
docker compose pull
docker compose build --no-cache
docker compose run --rm app bash
コンテナ内で実行コマンド
bundle install
bin/rails devcontainer

image.png

ファイルが生成されました!

生成されたファイル

.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": "workspaces",
  "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/devcontainers/features/node:1": {},
    "ghcr.io/rails/devcontainer/features/mysql-client": {}
  },

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

  // Use 'forwardPorts' to make a list of ports inside the container available locally.
  "forwardPorts": [3000, 3306, 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"
}
.devcontainer/compose.yaml
name: "workspaces"

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
    - mysql

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

  mysql:
    image: mysql/mysql-server:8.0
    restart: unless-stopped
    environment:
        MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
        MYSQL_ROOT_HOST: "%"
    volumes:
    - mysql-data:/var/lib/mysql
    networks:
    - default

volumes:
  redis-data:
  mysql-data:
.devcontainer/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/compose.yaml」を見て頂ければ分かるのだが、DBの設定情報が送られてない。
代わりに「.devcontainer/devcontainer.json」に記載されているため、それに合わせて修正する

app/config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>  
-  username: <%= ENV.fetch("DB_NAME") %>       # ←この行を修正
+  username: <%= ENV.fetch("DB_NAME") { 'root' } %>       # ←この行を修正
-  password: <%= ENV.fetch("DB_PASSWORD") %>   # ←この行を修正
+  password: <%= ENV.fetch("DB_PASSWORD") { '' } %>   # ←この行を修正
  host: <%= ENV.fetch("DB_HOST") %>           # ←この行を修正

devcontainerで立ち上げたあとに、コンテナ内のコンソールにて以下のコマンドを実行

bin/dev

上記URLで、下記の画面が表示されてれば完了です

image.png

さいごに

railsで作成されたdevcontainerは、本当に必要最低限という感じがします。
(イメージの肥大化などは無視で開発できればOKのように思えます)
ここからの拡張は、各プロジェクトでということなのでしょう。
sshに関しても、ssh-agentを利用しなさいということなのでしょう

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?