skip to content

In Rails 8.1, what does `rails new --devcontainer` generate, and how does the containerised app reach its PostgreSQL database?

level: middleimportance: nice to knowfreq 18%

answer

  1. off by default, opt in
  2. devcontainer.json, Dockerfile, compose.yaml
  3. postCreateCommand bin/setup --skip-server
  4. DB_HOST points at the postgres service
  5. bin/rails devcontainer for existing apps

basics

~10 s

rails new --devcontainer writes .devcontainer/devcontainer.json, a Dockerfile and compose.yaml; with -d postgresql, compose adds a postgres service, devcontainer.json sets DB_HOST to it, and database.yml reads DB_HOST as the host.

solid answer

~40 s

`--devcontainer` is off by default. When passed, the generator writes `.devcontainer/devcontainer.json`, `.devcontainer/Dockerfile` (based on `ghcr.io/rails/devcontainer/images/ruby`) and `.devcontainer/compose.yaml`, whose `rails-app` service runs `sleep infinity` so the editor can attach. Compose adds the services the app needs: `postgres` (image `postgres:16.1`, user and password `postgres`) for `-d postgresql`, a `selenium` service when system tests are on, and a Valkey service only with `--skip-solid`. `devcontainer.json` sets `DB_HOST` to the database service, forwards port 3000 and 5432, and runs `bin/setup --skip-server` as `postCreateCommand`. The PostgreSQL `database.yml` is regenerated so that, when `DB_HOST` is set, it uses that host with the `postgres` credentials. An existing app gets the same files from `bin/rails devcontainer`.

code

yaml · 24 lines
yaml
services:
  rails-app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
    command: sleep infinity
    depends_on:
    - selenium
    - postgres

  selenium:
    image: selenium/standalone-chromium
    restart: unless-stopped

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

go deeper

for a junior

Recall that --devcontainer is opt-in and writes three files under .devcontainer, and that bin/setup --skip-server runs after the container is created.

for a middle

Explain how compose services, DB_HOST and the regenerated database.yml connect the app to PostgreSQL, and which services appear for which options.

for a senior

Judge when a dev container pays off: onboarding, cloud workspaces and native-library pain versus image upkeep, and keeping it in step with the production Dockerfile.

for a principal

Decide whether a team standardises on dev containers, and how to keep one setup path for laptops, containers and CI.

## What a dev container is A **development container** is a Docker-based environment that an editor such as VS Code, or a cloud workspace, opens the project inside. The specification lives at containers.dev; a project describes its environment in a `.devcontainer/` folder and every developer gets the same Ruby, system libraries and services without installing them locally. Rails added a generator for it in 7.2; in 8.1 it is still **opt-in**: `rails new` writes nothing for it unless you pass `--devcontainer`. ## The files | File | Purpose | |---|---| | `.devcontainer/devcontainer.json` | name, compose file, service to attach to, features, environment, forwarded ports, `postCreateCommand` | | `.devcontainer/Dockerfile` | `FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION`, plus `ENV BINDING="0.0.0.0"` so the server listens outside the container | | `.devcontainer/compose.yaml` | the `rails-app` service and the backing services it depends on | This Dockerfile is separate from the production `Dockerfile` at the root of the app. The production one builds a slim runtime image for Kamal; the dev container one is a full development image. ## What `devcontainer.json` contains - `"dockerComposeFile": "compose.yaml"` and `"service": "rails-app"`, so the editor attaches to the app container. - **Features**, which are reusable install scripts: the GitHub CLI always, an Active Storage feature (image libraries) unless storage is skipped, a database client feature such as `postgres-client`, Node when the app uses it, and Docker-outside-of-Docker when Kamal is kept so `kamal` can build images. - **`containerEnv`**: `DB_HOST` for a server database, `KAMAL_REGISTRY_PASSWORD` passed through when Kamal is kept, and `CAPYBARA_SERVER_PORT` and `SELENIUM_HOST` when system tests are on. - **`forwardPorts`**: 3000, plus 5432 for PostgreSQL, plus 6379 if Redis is present. - **`"postCreateCommand": "bin/setup --skip-server"`**: install gems and prepare the database once the container exists, without starting the server inside the build. ## How the app finds PostgreSQL With `-d postgresql --devcontainer`: 1. `compose.yaml` adds a service named `postgres` using the `postgres:16.1` image, with `POSTGRES_USER` and `POSTGRES_PASSWORD` both `postgres` and a `postgres-data` volume; `rails-app` lists it under `depends_on`. 2. `devcontainer.json` sets `DB_HOST` to `postgres`, the service name, which Docker's network resolves. 3. The generator rewrites `config/database.yml` from the PostgreSQL template with a devcontainer block: when `ENV["DB_HOST"]` is set, the default section gains `host`, `username: postgres` and `password: postgres`. Outside the container `DB_HOST` is unset, so the same `database.yml` falls back to the local socket and role. With the default SQLite, no database service is added at all; the files live in `storage/` inside the mounted workspace. ## Other services - **`selenium`** (`selenium/standalone-chromium`) when system tests are on. With `--devcontainer`, `rails new` also writes `test/application_system_test_case.rb`, configured to drive the remote browser when `CAPYBARA_SERVER_PORT` is set. - **`redis`**, a `valkey/valkey:8` service, only when `--skip-solid` is passed and Action Cable or Active Job is kept. With the default Solid trio, the dev container has no Redis-compatible service. ## When it pays off - **Onboarding**: a new developer opens the folder and gets Ruby, the database client libraries, PostgreSQL and a browser for system tests without installing any of them. - **Native dependencies**: `pg`, image processing libraries and similar gems compile against the container's packages instead of whatever a laptop has. - **Cloud workspaces**: the same files drive hosted editors that understand the specification. The cost is upkeep: the dev image, the features and the production `Dockerfile` drift apart unless someone owns them, and container file-system performance on some hosts is slower than native. ## Adding one later For an existing app, `bin/rails devcontainer` boots the application, inspects it (which database adapter, whether Active Storage, Solid, Kamal and system tests are present) and runs the same generator. The 7.2 release notes describe the compose file as `docker-compose.yml`; the 8.1 generator writes `compose.yaml`, and the source is what an app actually gets.

  • Why does the generated `rails-app` service run `sleep infinity`?
    The container is a workspace, not a server. `sleep infinity` keeps it alive so the editor can attach and you can run `bin/dev`, tests or a console inside it. If the command were the Rails server, stopping the server would stop the container and drop your session.

  • Rails 8.1 generates a .devcontainer folder for every new app.
  • An existing app must be regenerated with rails new to get a dev container.
  • The dev container always runs Redis next to the app.
  • The dev container reuses the production Dockerfile at the app root.
  • Inside the container the app reaches PostgreSQL on localhost.

Assumes Rails 8.1.4; --devcontainer arrived in 7.2, and the 8.1 generator writes compose.yaml where the 7.2 notes say docker-compose.yml.