Basic configuration for running your Rails app in a Docker container on production
Ruby HTML JavaScript CSS CoffeeScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Basic configuration for running your Rails app in a Docker container in production

TLDR; Getting Started

To spin up the Rails Docker container in this example, simply run this command:

docker-compose up -d

This will pull down the Official Ruby Docker image, build a custom image with Rails setup and bring up a container with the Rails app running in it.

Access the Rails App

To access the Rails app in the container, go to localhost in your browser. This example maps port 3000 in the Rails container to port 80

More Documentation

The docker-compose.yml File

This is the file that defines how the Rails container should be built and brought up. There are additional comments in that file describing some of what's happening.

Rails Blog App

This sample configuration uses a simple generated Rails app called blog as described in the Rails Getting Started The app itself doesn't do anything except load a default welcome page. You can replace this sample app with your own Rails application. This blog app is only here to illustrate the Docker container configuration.

Ruby Docker Image

There used to be a Rails Docker image on Docker Hub, but that has been deprecated. Instead, it's recommended that we use the Ruby image and set it up to pre-install packages that support rails. You can see these package configurations in the Dockerfile. We're using Ruby 2.3.3 for this example. Official Ruby Docker Image

You can look at the deprecated Official Rails Docker Image here.

This is the rails image packaing from the Docker-Library docker-library/rails

Rails Dockerfile

The Dockerfile is specifically configured to use the official Ruby image, add packages and setup for our Rails application and save a new custom image.

Sqlite3 Databases Included

I've included the Sqlite databases in this repository for convenience. In this case they are empty and not setup. This is not best practice in an actual dev workflow.

Updating Rails App in Production

If you change a couple settings in your config/environments/production.rb file then you can replace files on your server and the Rails container will detect those changes without having to restart your containers. Here are the settings required for that to work:

config.cache_classes = false
config.eager_load = false

This is helpful if you want a simple way to update your app without downtime. There are more complex ways to accomplish continuous deployment goals, but this is the simplest and works in most cases. I've already made these changes to allow the example to work as described.