0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RailsデフォルトのDevcontainer でBundlerキャッシュを使う

Posted at

Rails newする時、--devcontainer オプションを指定することで、Devcontainer を使った開発環境を構築することができます。

この時構築されるDevcontainer の環境はbundle install されたgem のキャッシュが使われていません。
そのため、bundle install が走るたびにgem のダウンロードが発生するため、Devcontainer 立ち上げの度に時間がかかります。

compose.yml のvolume の設定を追加することで、bundle install されたgem のキャッシュをすることができますが、Devcontainer の場合は、Rails 公式から、features として提供されています。

.devcontainer/devcontainer.json
{
  "name": "rails_sample",
  "dockerComposeFile": "compose.yaml",
  "service": "rails-app",
  "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
  "features": {
    // ここに追加する
    "ghcr.io/rails/devcontainer/features/bundler-cache:1": {},
  },
  "forwardPorts": [
    3000,
    3306
  ],
  "postCreateCommand": "bin/setup --skip-server",
  "postStartCommand": "bundle check || bundle install"
}

実際にDevcontainer を立ち上げると、postCreateCommand で設定したbundler-cache のfeatures がなんか実行されていることがわかります。

Running the postCreateCommand from Feature 'ghcr.io/rails/devcontainer/features/bundler-cache:1'...

[45943 ms] Start: Run in container: /bin/sh -c sudo chown -R ${USER} /bundle

features/bundler-cache/devcontainer-feature.json を見るに、volumes を設定、インストール先のディレクトリの権限変更をfeatures を呼ぶことで実現してくれているようです。

Dockerfile でディレクトリ作って権限設定したり、volume を設定する手間が省けるので、便利だなというtipsでした。

Rails 公式で提供されているfeaturesなので、のちにデフォルトで入りそうな気もしますね :eyes:

また、Rails 公式のfeatures は他にもあるので、興味があれば見てみると良いかもしれません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?