Deploying a Rails Application with Docker

Vitaly Liber
3 min readMar 11, 2017

--

Recently I saw this video. I use docker-compose for development and thought about docker-compose in production. Today I started it.

The video doesn’t give all info about installing docker, docker-machine, and docker-compose. You should click the links to install them or follow the instructions below for Linux.

Install the docker community edition

  1. Fast install Docker for your system:

Ubuntu:

\curl -sSL https://gist.githubusercontent.com/vitalyliber/f071b2a4900e9789f54e1585fd63fc66/raw/363ae1e48b2cc1cb9211c8969aa07b991549417d/setup_docker.sh | bash

Debian:

\curl -sSL https://gist.githubusercontent.com/vitalyliber/e354ed06b1b018716fb17b5312800918/raw/becc6630b46aa3d11e4893831e32def3d4e4a29c/setup_docker.sh | bash

Windows:

https://download.docker.com/win/stable/InstallDocker.msi

Virtual sever

In video above we saw than author use digitalocean. But Hetzner suggest cheaper virtual servers. I bought one.

Configuring a new Rails app for docker

  1. Creating a new rails app.
rails new noragami --database=postgresql

2. Add Dockerfile.prod for production environment.

FROM ruby:2.3

RUN apt-get update -yqq \
&& apt-get install -yqq --no-install-recommends \
postgresql-client \
&& rm -rf /var/lib/apt/lists

WORKDIR /usr/src/app
COPY Gemfile* ./
RUN bundle install
COPY . .
RUN rake assets:precompile

EXPOSE 3000
CMD rm -rf tmp/pids/server.pid \
&& rails server -b 0.0.0.0 -p 3000

3. Add docker-compose.prod.yml file for production environment.

version: "2"

volumes:
db_prod: {}
services:
db_prod:
image: postgres
env_file: .env
volumes:
- db:/var/lib/postgresql/data

app:
build:
context: .
dockerfile: Dockerfile.prod
env_file: .env
environment:
RAILS_ENV: production
ports:
- "3000:3000"
depends_on:
- db_prod

4. Add .env file.

POSTGRES_USER=noragami_db
POSTGRES_PASSWORD=pass123
POSTGRES_HOST=db
SECRET_KEY_BASE=xxx
RAILS_SERVE_STATIC_FILES=true

5. Replace your config/database.yml

default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3

test:
<<: *default
database: noragami_dev

production:
adapter: postgresql
encoding: unicode
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
host: <%= ENV["POSTGRES_HOST"] %>
database: noragami_prod
timeout: 5000

Create Docker host

Create machines using an existing VM/Host with SSH.

I named my host ‘Summer’. You must add your Hetzner VPS ip address to generic-api-address.

docker-machine create \   
--driver generic \
--generic-ip-address=203.0.113.81 \
--generic-ssh-key ~/.ssh/id_rsa \
--generic-ssh-user root \
summer

More about generic.

Build and deploy App

You can get remote host info.

docker-machine env summer

1. Run this command to configure your shell:

eval $(docker-machine env summer)

2. Starting db.

docker-compose up -d db

3. Build an App.

docker-compose build app

4. Create a db and a migration.

docker-compose -f docker-compose.prod.yml run --rm app rake db:create db:migrate

5. Create a secret key and copy it to .env.

run --rm noragami_app rake secret

6. Build and start.

docker-compose build app
docker-compose -f docker-compose.prod.yml up -d app

Check application on your vps ip and port 3000

http://203.0.113.81:3000

You will see this error

Your Rails app doesn’t have any views

That means you successfully deployed your app with docker.

My Instagram:

https://www.instagram.com/vitalyliber

My Courses:

Deploy apps with Dokku (Russian)

--

--

Vitaly Liber

Full Stack Engineer (React Native, Expo, Next.js, Ruby on Rails)