RAILS API ON AWS – PART 3: Docker

Part 3 is about dockerizing Rails API-only app with NGINX and PostgreSQL (PostGIS).

 

List of Posts:

  1. part 1: Local server setup
  2. part 2: Basic Rails App with PostGIS
  3. part 3: Docker
  4. part 4: AWS setup
  5. part 5: AWS setup & deploy

 

 

  1. Introduction
  2. Project Structure
  3. Dockerfiles
  4. Run
  5. Git
  6. Troubleshooting

1. Introduction

In part 1 you can find instructions how to install Docker if you haven’t done this yet.

I’m going to run Rails App, NGINX and PostgreSQL as 3 separate containers on my local development machine. You will be able to run each of them with Docker Engine and all of them with Docker Compose. When we deploy to Elastic Beanstalk, we will need only two out of them: NGINX and Rails App, because our PostgreSQL with PostGIS will be running on AWS RDS.

Github: https://github.com/tomaszantas/rails-api-on-aws/releases/tag/part3

2. Project structure

In next chapter, I setup highlighted files.

 

3. Dockerfiles

 

Rails App

 

app/Dockerfile

app/config/puma.rb:

app/script/wait_for_db.sh:

Using script wait_for_db.sh we can wait until Rails app can connect to database.

app/config/database.yml:

The most important thing here is a setting host to db. (db comes from docker-compose.yml)

 

NGINX

 

nginx/nginx.conf:

nginx/Dockerfile:

 

Docker Compose

 

docker-compose.yml:

Replace xxx (database user) and yyy (database password) as you like.

 

4. Run

To start containers, be sure that your local nginx and PostgreSQL are not running. It’s important, because they can occupy specific ports (80 and 5432):

Run Docker Compose:

 

5. Git

Our Docker containers may create new folders: logs and data. Let’s add them to .gitignore:

 

6. Troubleshooting

docker-compose up >>> FATAL:  role “xxx” does not exist   /   ActiveRecord::NoDatabaseError: FATAL:  role “xxx” does not exist

Probably you set wrong credentials to your database in docker-compose.yml or database.yml. However, you might fix them already, but the error still exists. The reason is

However, you might fix them already, but the error still exists. The reason is that docker-compose reuses some elements. So when your configuration was wrong on the first run, it may be still wrong. And plain “docker-compose build” and “docker-compose up” won’t fix it. The solution is to clear everything that docker-compose has created:

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!