What is Devcontainer? Short and easy guide on how to use it

As an experienced developer, you're always looking for tools to streamline your workflow and improve collaboration. Enter DevContainers - a powerful solution that's changing the game for development environments. In this article, we'll explore what DevContainers are, their benefits, and how to implement them in your projects, with a focus on Rails 7.2's built-in support.

What is a DevContainer?

A DevContainer, short for development container, is a fully-featured development environment encapsulated within a container. It allows you to:

  1. Run your application in an isolated environment
  2. Separate tools, libraries, and runtimes specific to your codebase(database,Redis, Minio, etc)
  3. Facilitate continuous integration and testing

DevContainers can be run locally or remotely, in private or public clouds, and are supported by various tools and editors.

Setting Up Your DevContainer Environment

To get started with DevContainers, you'll need three key tools:

  1. Docker
  2. Visual Studio Code (VS Code) or any supported IDEs like RubyMine
  3. rails gem

Installing Docker

Docker is the foundation for running DevContainers. To install Docker:

  1. Visit the official Docker website (www.docker.com)
  2. Follow the installation instructions for your operating system
  3. Launch the Docker application to start the Docker engine

Setting Up VS Code

Visual Studio Code is the recommended editor for working with DevContainers. To set it up:

  1. Download and install VS Code from the official website
  2. Install the "Dev Containers" extension from the VS Code marketplace

This extension allows you to open folders inside containers and leverage VS Code's full feature set.

Creating a Rails Application with DevContainer Support

Rails 7.2 introduces built-in support for DevContainers. Here's how to use it:

$ rails new myapp --devcontainer
$ cd myapp

This command generates a new Rails application named myapp with a pre-configured DevContainer setup.

Understanding the Generated DevContainer Files

The --devcontainer flag creates three important files:

  1. .devcontainer/devcontainer.json: Defines the DevContainer configuration
  2. .devcontainer/Dockerfile: Specifies the container image and basic setup
  3. .devcontainer/compose.yaml: Typical docker-compose file to set up containers

Let's take a closer look at 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": "myapp",
  "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": {
    "CAPYBARA_SERVER_PORT": "45678",
    "SELENIUM_HOST": "selenium",
    "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"
}

I think the comments are self-explanatory, this configuration:

  • Names the container
  • Sets the workspace folder
  • Adds ports for the app and services for Capybara, Redis, Selenium, DB, etc.
  • Specify environment variables
  • Runs bundle setup after creating the container

Opening Your Project in a DevContainer

To use your new DevContainer:

  1. Open the project folder in VS Code
  2. When prompted, click "Reopen in Container"
  3. VS Code will build and start the DevContainer

Once inside the container, you can use the integrated terminal to run Rails commands:

$ rails --version
Rails 7.2.0
$ rails s 
# now you should see a popup from IDE to open page in browser

Benefits of Using DevContainers

With that setup, you can easily predict the safe time to introduce a new person into any project, or when you're buying a new computer.

  1. Consistency: Ensures all developers work in the same environment
  2. Isolation: Keeps project dependencies separate from your local system
  3. Portability: Easily share and reproduce development environments
  4. Onboarding: New team members can start coding quickly
  5. CI/CD Integration: Use the same environment for development and testing

Summary

DevContainers offer a powerful way to standardize and simplify development environments. With Rails 7.2's built-in support, it's easier than ever to leverage this technology in your Ruby on Rails projects. By encapsulating your development environment in a container, you ensure consistency across your team and streamline the development process.

Happy devcontenering!