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.
Dev containers can be run locally or remotely, in a private or public cloud, in a variety of supporting tools and editors.
Importance of devcontainers
-
Consistency Across Environments DevContainers ensure identical development environments for all team members, reducing bugs from setup inconsistencies.
-
Simplified Onboarding New team members can quickly start coding without extensive setup, as the DevContainer configures everything automatically in VS Code.
-
Dependency Management Isolate project dependencies within the container, preventing conflicts with other projects or system-wide settings.
-
Portability Easily share and replicate development environments across different machines, ideal for remote teams and occasional contributors.
-
Integration with CI/CD Pipelines Align development, build, and test environments, leading to more reliable builds and fewer environment-specific issues.
Dev containers before Rails 7.2
Before Rails 7.2, devcontainer files were not generated with new Rails applications.
To use devcontainer with Rails prior to 7.2, we had to manually set up the necessary files and configuration.
The basic steps to add a dev container to a Rails application before 7.2 would be:
-
Create a
.devcontainer
directory at the top level of the Rails application repository. -
Inside the
.devcontainer
directory, create adevcontainer.json
file that specifies the image and features to use for the dev container. -
Optionally, we could also create a Dockerfile in the
.devcontainer
directory to further customize the dev container image. -
Finally, we would need to initialize the devcontainer using either VS Code or the devcontainer CLI.
Dev containers after Rails 7.2
Rails 7.2 now ships with devcontainers as an opt-in feature.
Adding devcontainer on a new rails app
Adding devcontainer to an existing rails app
Executing the above command will generate a .devcontainer
folder
and its contents by default.
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
The default .devcontainer
folder comes with these files compose.yaml
, devcontainer.json
and Dockerfiler
If any of these options are skipped in the app setup they will not be included in the container configuration.
Here is the default .devcontainer/compose.yaml
Here is the default .devcontainer/devcontainer.json
Here is the default .devcontainer/Dockerfile
Conclusion
With the recent addition of DevContainer support in Rails, they are a game-changer for modern software development, addressing many of the pain points associated with environment setup and dependency management.
By leveraging the power of Docker and the flexibility of VS Code, DevContainers offer a robust solution for creating consistent, portable, and efficient development environments.
Whether we are part of a large team or a solo developer, adopting DevContainers can significantly enhance our workflow and productivity.
References