Coder Social home page Coder Social logo

backpackerhh / dockerized-rails-template Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 40 KB

Dockerized Rails template aimed to be used in coding challenges

Ruby 78.22% Makefile 3.74% Dockerfile 3.90% CSS 0.06% HTML 13.61% Shell 0.47%
ruby docker linter rails rails-application rspec rubocop ruby-on-rails testing coding-challenge template

dockerized-rails-template's Introduction

Dockerized Rails template

This template aims to create a simple Rails application with Docker, testing tools and linter already configured.

Especially intended for coding challenges.

Up and running

Add database configuration:

$ cp config/database.yml{.sample,}

Change the database name for development and test environments. Look for the FIXME comments inside that file.

Specify the same database name as the default value of DB_NAME variable in Makefile, keeping the environment as suffix.

In docker-compose.yml file, specify the desired image name.

Commands

The first time you will need to build the application:

$ make start-build [SERVICES="<whitespace-separated services>"] [WORKDIR="<absolute path>"] [PORT=<port>]

Examples:

$ make start-build
$ make start-build SERVICES="app db"
$ make start-build WORKDIR=/rails
$ make start-build PORT=3001

Combine options as needed.

The server will be running on port 3000 by default, unless otherwise specified.

Generate config/master.key and config/credentials.yml.enc with following command:

$ make edit-credentials

VIM is used inside the container to edit the encrypted credentials.

Restart server without restarting containers:

$ make restart-server

Other useful commands

Start containers without building them:

$ make start [PORT=<port>]

Restart containers:

$ make restart-build [PORT=<port>]
$ make restart [PORT=<port>]

Stop containers:

$ make stop

Destroy containers:

$ make destroy

Check Makefile for other useful commands.

Application

The application has been generated using latest Rails version at the moment of writing. I'll try to keep it updated as new versions are released.

The same goes for Ruby.

Ensure you use the same version in .ruby-version file and the RUBY_VERSION argument in every Dockerfile.

The application was generated with following command:

$ docker compose exec app bundle exec rails new . --database postgresql --skip-git --skip-keeps --skip-asset-pipeline --skip-turbolinks --skip-test --skip-system-test --skip-spring --skip-bootsnap --skip-jbuilder --skip-javascript

If you need an only API application, configure it accordingly. Follow the official documentation.

Note that you could remove any directory or file that you know you won't need.

Database

This code assumes Postgres is used. In case you prefer to use another database, change the configuration accordingly.

Run following command:

$ docker compose exec app rails db:system:change --to=<new database>

Change it manually in other Dockerfiles.

More details here.

Testing

RSpec is the testing framework of choice.

Check the configuration in following files to adapt it if necessary:

  • .rspec
  • spec/rails_helper.rb
  • spec/spec_helper.rb

FactoryBot is used instead of fixtures.

Database Cleaner Adapter for ActiveRecord is used to clean the database after running the test suite. For that, you need to specify the type database.

Example:

RSpec.describe "Create user", type: %i[request database] do
  # omitted
end

Linter

RuboCop is the linter of choice.

Check the configuration in .rubocop.yml file to adapt it if necessary.

Possible improvements

A non-root user is configured to run the application. To be able to successfully add new gems to the project, the owner of the Bundler path, defined in every Dockerfile, is changed in the final stage of the build process. Notice that that process is quite slow at this moment.

dockerized-rails-template's People

Contributors

backpackerhh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.