paint-brush
Dockerizing an existing Rails-Postgresql app with Docker composeby@sureshvignesh
8,318 reads
8,318 reads

Dockerizing an existing Rails-Postgresql app with Docker compose

by Vignesh SureshJanuary 14th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A quickstart to dockerizing your existing Rails-Postgresql application with Docker compose
featured image - Dockerizing an existing Rails-Postgresql app with Docker compose
Vignesh Suresh HackerNoon profile picture

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'services:db:image: postgresvolumes:





  • ./tmp/db:/var/lib/postgresql/dataweb:build: .command: bundle exec rails s -p 3000 -b '0.0.0.0'volumes:


  • .:/myappports:


  • "3000:3000"depends_on:

  • 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

password:

pool: 5

development:


<<: *defaultdatabase: 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.