Running Rails 5.0.1 + PostGIS + puma + nginx in production using docker-compose 2

In this post we will learn to

Configure docker compose for a production setup of a PostGIS + Ruby on Rails 5.0.1 application with nginx as proxy server

We will build on the development version of the application we create in my last post here. If you want to follow along then make sure you have that one done.

In this post we will create the production

  1. Dockerfile for production version of the Rails app
  2. .env file for production
  3. nginx proxy configuration for the Rails app (NEW)
  4. docker-compose.yml file for production
  5. entrypoint script

TL;DR just run this script:

So what did we do? Most of it is pretty self explanatory, but the tricky part is the nginx configuration and mounting of the volume where the rails assets are stored and accessible for nginx. Note the following docker-compose.yml lines in nginx service. This says when running nginx container please mount any volumes you have from the app container.

...
volumes_from:
- app

And if we look in the app service

...  
volumes:
- assets:/usr/app/${APP_NAME}/public/assets
- nginx_config:/etc/nginx/conf.d

This means the app container mounts a named volume called assets and nginx_config at the defined paths; and finally the top-level volume declarations with:

...
volumes:
assets:
external: false
data:
external: false
nginx_config:
external: false

this makes sure the assets are mounted where Rails would normally put assets. So when we run rake assets:precompile assets end up in a share volume and are available to other containers. Read more about it here: https://docs.docker.com/compose/compose-file/#/volumes-volumedriver

If we look at our Dockerfile we will see:

RUN mkdir -p /etc/nginx/conf.d/
COPY ./containers/production/nginx.conf /etc/nginx/conf.d/default.conf

This ensures that our nginx configuration is placed in the shared volume and visible to the nginx container. Similarly our entrypoint script copies the config to shared volume as well as runs the asset compilation again. This is to make sure that asset changes and nginx config changes are reflected on restart.

When done running this script just run docker-compose up and make sure that your SERVER_NAME is in your /etc/hosts file.

In the next post we learn to ship the Rails app and deploy to DigitalOcean.

Show your support

Clapping shows how much you appreciated Shoaib Burq’s story.