loading...
Cover image for Dockerize a new Rails application

Dockerize a new Rails application

vshl profile image Vishal ・2 min read

A simple guide to get started with Rails and Docker.

Deploying a Ruby on Rails app to Docker is useful for distributing and containerizing your Ruby gems. This is helpful in particular if you have many projects that have different versions of both Ruby and Rails on your local system. Secondly, it becomes trivial to distribute your application without worrying about incompatibilities and unforeseen problems.

The following guide helps you setup a new project in Rails to build and deploy the image to a Docker container.

At the minimum you would need 5 files to get started:

  1. Dockerfile

  2. docker-compose.yml

  3. .dockerignore

  4. Gemfile

  5. Gemfile.lock

Dockerfile

For the Dockerfile, you will need to build a Ruby image that would install nodejs for Rails to run. The file takes the Gemfile and Gemfile.lock and copies them to the WORKDIR . The command bundle install is then run within the image to install the gems listed in the Gemfile.

Gemfile

The Gemfile will initially only contain the Rails gem:

source 'https://rubygems.org' 
git_source(:github) { |repo| "https://github.com/#{repo}.git"

gem 'rails', '~> 5.2.4', '>= 5.2.4.2'

Gemfile.lock

Gemfile.lock will be an empty file. Create one.

docker-compose.yml

docker-compose will be the main file that sets up your debugger, server command to be executed and the volume share between your local machine and the docker image and the port to be exposed.

.dockerignore

    .git/
    .gitignore
    log/*
    tmp/*
    db/*.sqlite3
    db/*.sqlite3-journal

Once the files are prepped, next is to build the image and setup a new Rails application in the image. In a terminal run:

docker-compose up -d

The command will run through the docker-compose.yml file and build the image. Once it’s complete you will have an image with the Rails gem installed. Next is to create a new Ruby on Rails application. We will use the created image to run a command in a container.

docker-compose run web rails new . -GMOPCSJT --force --no-deps

The arguments -GMOPCSJT are optional in case you will need to skip creating additional boilerplate files and directories for -G git, -M action-mailer, -O active-record , -P puma , -C action-cable, -S sprockets , -J javascript, -T test-cases. Feel free to add the arguments that you will require for your projects.

The command will create a Rails project in the WORKDIR of the Docker image. Once it’s complete, it is suitable to re-build your Docker image by running the command:

docker-compose up -d --build

On completion, you will have a server running on port 3000. http://localhost:3000

That’s it!

I created a git repo so one can clone and get started right away:

git clone git@github.com:vshl/rails-docker-skel.git

I’ll continue to update that repo and this blog post as I find better ways to follow good practices.

Posted on by:

vshl profile

Vishal

@vshl

Love things Linux, Ruby and CLI

Discussion

markdown guide