Skip to content

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:

FROM ruby:2.5.3
#RUN apt-get update
#RUN apt-get install -y yarnpkg
#RUN ln -s /usr/bin/yarnpkg /usr/local/bin/yarn
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /workspace
COPY Gemfile /workspace/Gemfile
COPY Gemfile.lock /workspace/Gemfile.lock
RUN gem install bundler
RUN bundle install

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:

version: "3.2"
services:
  db:
    image: postgres
    volumes:
      - ./container_db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
    ports:
      - 5432:5432
  app:
    build:
        context: .
        dockerfile: Dockerfile
    volumes:
      - type: bind
        source: .
        target: /workspace
    working_dir: /workspace
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    ports:
      - "3000:3000"
    depends_on:
      - db

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:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5
  min_messages: warning

development:
  <<: *default
  database: filentorydev


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: filentorytest

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

Run Rails

We can now start both containers with this command:

docker-compose up

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.