A development container (or dev container for short) allows us to use a container as a full-featured development environment.
It can be used to run an application, to separate tools, libraries, or runtimes needed for working with a codebase, and to aid in continuous integration and testing.
The dev containers can be run locally or remotely, in a private or public cloud, in a variety of supporting tools and editors.
Before
Rails 7.2 ships with dev containers as an opt-in feature.
Adding dev container on a new rails app
rails new <app_name> --devcontainer
Adding dev container to an existing rails app
bin/rails devcontainer
The .devcontainer
folder includes everything needed to boot the app and do development in a remote container.
The container setup includes:
- A redis container for Kredis, ActionCable etc.
- A database (SQLite, Postgres, MySQL or MariaDB)
- A Headless chrome container for system tests
- Active Storage configured to use the local disk and with preview features working
Here is the default .devcontainer/compose.yaml
name: "app_name"
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
networks:
- default
# 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.)
ports:
- 45678:45678
depends_on:
- selenium
- redis
selenium:
image: seleniarm/standalone-chromium
restart: unless-stopped
networks:
- default
redis:
image: redis:7.2
restart: unless-stopped
networks:
- default
volumes:
- redis-data:/data
volumes:
redis-data:
Here is the default .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": "app_name",
"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/sqlite3": {}
},
"containerEnv": {
"CAPYBARA_SERVER_PORT": "45678",
"SELENIUM_HOST": "selenium",
"REDIS_URL": "redis://redis:6379/1"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000, 6379],
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root",
"mounts": [
{
"type": "bind",
"source": "/Users/prasanthchaduvula/Documents/rails",
"target": "/Users/prasanthchaduvula/Documents/rails"
}
],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "bin/setup"
}
Read about the new dev container support introduced in Rails 7.2 in our previous blog Rails 7.2 Adds Support For Devcontainer.
After
Rails 8 does not include redis by default in generated dev containers.
In Rails 8, newly generated apps will use the Solid Queue and Solid Cache gems by default, which do not depend on Redis.
When generating a dev container for an existing app, Rails will not include redis if either of the solid gems are in use.
In the newly generated dev container
files, Redis won’t be present.
Here is the default .devcontainer/compose.yaml
name: "rails8_alpha_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:
- selenium
selenium:
image: selenium/standalone-chromium
restart: unless-stopped
Here is the default .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": "rails8_alpha_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/sqlite3": {}
},
"containerEnv": {
"CAPYBARA_SERVER_PORT": "45678",
"SELENIUM_HOST": "selenium"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000],
// 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 --skip-server"
}
Rails include Redis if --skip-solid
is used when generating an app that use ActiveJob or ActionCable.