Vignesh Suresh


Dockerizing an existing Rails-Postgresql app with Docker compose

A quickstart to dockerizing your existing Rails-Postgresql application with Docker compose

Dockerfiles — Define the project

Create Dockerfile in the app’s root directory, the project is defined by adding the dependencies to the Dockerfile.

FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

Make sure add the specific version of ruby that your application uses

Docker-compose — Build the project

Now, create docker-compose.yml file in the root directory. This file does the magic of building your app from the current directory and running the pre-built postgresql image with the web app

version: '3'
image: postgres
- ./tmp/db:/var/lib/postgresql/data
build: .
command: bundle exec rails s -p 3000 -b ''
- .:/myapp
- "3000:3000"
- db

Database.yml — Configure DB

Now, Change the host to ‘db’ in your database.yml file to point to the docker service

default: &default
 adapter: postgresql
 encoding: unicode
 host: db
 username: postgres
 pool: 5
 <<: *default
database: myapp_development

The following tasks can be completed be completed by running the respective commands

Build Docker Image

docker-compose build

DB — Create and Migrate

docker-compose run web rake db:create db:migrate

Accessing Rails Console

docker-compose exec web rails console

Start application

docker-compose up

You should now be able to browse to http://localhost:3000 and see your application running.

More by Vignesh Suresh

Topics of interest

More Related Stories