How to Create a Dev Container for Rails Apps

After the preparations of the last weeks, it is now time to put everything together and run our Rails application in a dev container.

 

Prerequisites

Rails is a Ruby web framework and therefore needs a Ruby image as a starting point. Please follow my post “Dev Container for Ruby Apps” to figure out what version of Ruby you need.

I use PostgreSQL as a database for my Rails apps. Please follow the steps described in “Dev Container for PostgreSQL” to create a container with PostgreSQL.

 

Create a Dockerfile

The Dockerfile is similar to the one we created for Ruby, but it installs some additional tools for Rails:

If you need yarn you can uncomment lines 2 to 4.

 

Create a docker-compose.yaml file

Our Rails app only works with a database. Therefore, our docker-compose.yaml file creates two containers and makes sure that the database container is up and running before it starts our Rails dev container:

The name we give the service for the database (db) is important. Inside Docker we can use this as the host name when we connect from one container to another.

 

Change the database settings

Since we no longer have a database that runs on the same machine / container as when we developed the Rails application in the past, we need to make a change to the config/database.yml file. Add a new default block and replace the configuration for the development and the test database to match this snipped:

Do not forget to create the two databases in your PostgreSQL container!

 

Run Rails

We can now start both containers with this command:

 

Make changes to the Rails app

We can now connect with Visual Studio Code to our dev container and develop new features for our Rails application. The auto-reload feature of Rails works as always and the same is true for the test environment.

 

Next

So far we looked at how to get old versions to run in a dev container. We can use dev containers to develop with current or new versions as well. Next week we look at SQL Server and how we can run that product in a dev container.

2 thoughts on “How to Create a Dev Container for Rails Apps”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.