Rails 5.2 with Docker & Docker Compose

This blog will help you to deploy your rails 5.2 application in docker with help of docker compose.

Rails application developed by my colleague to explore some of rails 5 feature . I forked it https://github.com/sivakumarbdu/rails5.2_credentials_and_csf.

Lets get started by cloning application

git clone    git@github.com:sivakumarbdu/rails5.2_credentials_and_csf.git

Next create Dockerfile and docker-compose.yml and add it to the application root.

Lets create Dockerfile.

Dockerfile

FROM ruby:2.5.1
ENV APP_HOME /myapp
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY . $APP_HOME
ENV BUNDLE_PATH /bundle
RUN bundle install
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs
CMD tail -f /dev/null

Then create docker-compose.yml. I am using postgres as database.

docker-compose.yml

version: "3.3"
services:
db:
image: postgres:9.4.1
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=password
volumes:
- /usr/volumes/pgdata/etc:/etc/postgresql:Z
- /usr/volumes/dev/volumes/pgdata/log:/var/log/postgresql:Z
-/usr/volumes/pgdata/data:/var/lib/postgresql/data:Z
app:
build: .
command: bundle exec rails s -b 0.0.0.0
environment:
- BUNDLE_PATH=/bundle
ports:
- 3000:3000
links:
- db
volumes:
- bundle:/bundle:Z
- .:/myapp:Z
volumes:
bundle:

All bundle will be installed at /bundle director at the container and mounted to host. Port 3000 forwarded to host from container.

Finally create database.yml

development:
adapter: postgresql
encoding: unicode
database: demo
username: postgres
password: password
pool: 5
host: db

We are done, build and deploy the application.

docker-compose build .
docker-compose up

Navigate to http://localhost:3000 to see the application running.