No More ‘It Works on My Machine’ in Rails 8 with Dev Containers

A Guide to Consistent Development Environments

Tushar Adhao
4 min readDec 19, 2024

Maintaining a consistent development environment is one of the biggest challenges in modern software development. Whether you’re working solo or as part of a team, discrepancies between environments can lead to frustrating bugs, wasted time, and complex setup processes. Dev Containers are a powerful solution to this problem, offering a way to encapsulate your development environment in a container for easy, repeatable setups.

Where every developer’s environment is as colorful and consistent as a rainbow!

In this blog post, we’ll introduce Dev Containers, explain their benefits, and explore how they can be used to streamline development workflows in Rails 8 projects.

What are Dev Containers?

A Dev Container is a development environment packaged inside a Docker container, providing a consistent setup for all developers on a project. With Dev Containers, you define your development environment — dependencies, tools, libraries, and configuration settings — in a container that runs the same way across different machines.

This eliminates the “it works on my machine” problem by ensuring everyone is using the same environment. Dev Containers integrate with Visual Studio Code (VSCode) through the Remote — Containers extension, allowing developers to work within a containerized environment directly from their code editor.

Why Use Dev Containers in Rails 8?

There are several reasons why adopting Dev Containers in your Rails 8 project can be beneficial:

  1. Consistency Across Environments
    Dev Containers ensure that the development environment is the same for every team member, regardless of the operating system they use or the specific configuration of their local machine. This means the same versions of Ruby, Rails, Node.js, and other dependencies are used, leading to fewer compatibility issues.
  2. Simplified Onboarding
    Setting up a development environment can often take hours, especially for new developers. With a Dev Container, new team members can start contributing with minimal setup. Once Docker and VSCode are installed, they can launch the container and immediately have access to the full development environment.
  3. Isolated Environments
    Dev Containers provide a clean, isolated environment that doesn’t interfere with your local machine’s settings. This makes it easy to manage dependencies and configurations without worrying about polluting your global system setup.
  4. Cross-Platform Compatibility
    Whether you’re working on Windows, macOS, or Linux, Dev Containers help ensure that the environment behaves the same way on every platform. This is especially useful for cross-platform development teams.
  5. Integration with CI/CD Pipelines
    Dev Containers are not limited to local development. You can use them in your Continuous Integration (CI) and Continuous Delivery (CD) pipelines to ensure that tests and deployments are executed in the same environment as local development.

Note: Dev containers are not tied to any specific version of Rails, but with the release of Rails 8, they have become easier to use thanks to community-driven solutions and the adoption of container-based development workflows.

How to Use Dev Containers with Rails 8

While Rails 8 does not include built-in support for Dev Containers, you can easily set up a Docker-based environment to run your Rails project inside a container. Here’s the basic idea:

  1. Create a Dockerfile:
    The first step in setting up a Dev Container is defining your environment in a Dockerfile. This file contains instructions for building your container, including installing Ruby, Rails, Node.js, and other dependencies needed for your project.
  2. Configure VSCode:
    The next step is creating a devcontainer.json configuration file. This file tells VSCode how to launch and configure the container when you open the project. It can specify the Dockerfile, set up environment variables, install extensions, and run commands to install dependencies like Ruby gems and JavaScript packages.
  3. Work Inside the Container:
    Once everything is configured, you can open your Rails project in VSCode and use the Remote — Containers extension to “Reopen in Container”. This will launch the container and mount your project files into it, so you can begin development right away.
  4. Leverage the Container for Testing and Deployment:
    Beyond local development, you can use Dev Containers for testing your application in a consistent environment. It’s also possible to use the same configuration in your CI/CD pipeline to ensure that your tests, builds, and deployments are all executed in an identical environment.

Benefits of Using Dev Containers with Rails 8

  1. Consistency Across Machines: Developers working on different operating systems or configurations can all work in the same environment.
  2. Simplified Environment Setup: New team members can quickly get up and running with a pre-configured environment.
  3. Better Collaboration: With everyone using the same setup, collaboration becomes smoother, as you won’t have to worry about machine-specific issues.
  4. Easier Testing and Deployment: By using containers for testing and deployment, you ensure that the code behaves consistently across environments.

Conclusion

Dev Containers offer a simple and effective way to manage development environments, ensuring consistency, portability, and ease of collaboration. While Rails 8 doesn’t directly provide Dev Container support, integrating them into your Rails project is straightforward. By using Dev Containers with Docker and VSCode, you can ensure that all developers, whether working locally or remotely, are using the same setup — leading to fewer environment-related bugs, faster onboarding, and a smoother development process.

Start using Dev Containers today and make your Rails 8 development process more efficient and consistent!

--

--

Tushar Adhao
Tushar Adhao

Written by Tushar Adhao

Software artist spreading nuggets of coding gold and sometimes philosophy too.

No responses yet