Skip to content
/ rails-docker Public template

A barebones, dockerised dev environment for Rails

License

Notifications You must be signed in to change notification settings

danwhitston/rails-docker

Repository files navigation

Dockerised Rails Development Environment

This repository acts as a base for developing Rails applications using Docker. It uses docker-compose to set up and run a Postgres database in a separate container, running alongside the Rails application container. The production environment will require further setup before it can start functioning.

Contents

The current versions of various dependencies are:

The Dockerfile, docker-compose, and Rails application files are all in the repo root. A Rails app has already been generated and is present in the repository, to save time on the manual setup steps detailed at the end of this README.

Usage

This repo is meant to contain a generic Rails setup, not the particular application that you intend to build with it. To use this, either visit https://github.com/danwhitston/rails-docker and click 'Use this template', or git clone it, and start work, or git clone it, start work and set the remote to a new destination. Either way, you should rewrite this README to give instructions for working with your own application.

There are two methods of bringing up a development instance of the Docker setup.

Method with VSCode dev container integration

A .devcontainer folder has been added, with settings that appear to work for bringing up a dev environment. To use this, open the root folder in VSCode, ensuring that various necessary extensions are installed. They need further tweaking so that we don't need to get a CLI and manually run rake db:setup, then bundle exec rails s -p 3000 -b '0.0.0.0' to get a server at localhost:3000.

Method without VSCode dev container integration

  • First ensure that Docker is installed on your system, along with docker-compose
  • Run docker-compose build to set up the Rails and Postgres Docker instances
  • Run docker-compose up to start a development instance
  • Your shell should show the usual Rails and Postgres command line outputs and should not give you a command line
  • Create the test and dev databases from another shell window with docker-compose run web rake db:setup
  • Navigate to localhost:3000 and you should see a Yay! You're on Rails welcome screen

After creating a scaffold for the first time, it may be necessary to:

  1. Set default: extract_css: true, development: dev_server: hmr: true in webpacker.yml
  2. Run docker-compose run --rm web bundle exec rake webpacker:install to get webpack working

By uncommenting some lines in the docker-compose.yml, you can also enable an Adminer instance to examine your databases.

Pending tasks

  • Add IDE or editor configuration and documentation, so it's easier to develop with
  • Convert the setup to an initial-build script, to remove code from the repository and rebuild from fresh with each install
  • Use Ruby-3.0-Alpine as the base and add further packages as necessary
  • Add an example production config

How it was created

Useful for recreating or updating:

  • To start, use the following files from this repo
    .dockerignore
    .gitignore
    docker-compose.yml
    Dockerfile
    entrypoint.sh
  • Create a ./Gemfile with just the following lines
    source 'https://rubygems.org'
    gem 'rails', '~>6.1.0'
  • Use touch Gemfile.lock to create an empty lockfile, needed for the Docker build to work correctly
  • Create the rails app with docker-compose run --no-deps web rails new . --force --database=postgresql --skip-test
  • Use sudo chown -R $USER:$USER . to fix user ownership of the created files
  • Set up database connection in ./config/database.yml
      default: &default
        adapter: postgresql
        encoding: unicode
        host: db
        username: postgres
        password: password
        pool: 5
  • Add the following code to ./Gemfile to install RSpec-Rails. You can do this either with docker-compose run --no-deps bundle add rspec-rails --version "~> 4.0.2" --group "development, test" which also runs bundle install, or with the manual addition below
    # Run against the latest stable release
    group :development, :test do
      gem 'rspec-rails', '~> 4.0.2'
    end
  • Then use docker-compose build to install RSpec-Rails, followed by docker-compose run --no-deps web rails generate rspec:install to set up RSpec test generation
  • Use sudo chown -R $USER:$USER . again to fix ownership of the remaining created files
  • Finally, run docker-compose up to run the two Docker instances, and in another shell run docker-compose run web rake db:setup to set up the test and development databases in the PostgreSQL instance, run migrations, and seed content

This was based on https://docs.docker.com/compose/rails/ but required several changes to get working on the current Ruby and Rails versions, as NodeJS / NPM / Yarn did not install correctly.

About

A barebones, dockerised dev environment for Rails

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published