Dockerize Rails

What is Docker?

Docker is an application that helps developers setup, share, and run other applications, like web apps, database servers, and more. Docker runs applications inside software containers, which provide a lightweight, isolated, and repeatable environment for running applications.

What are the benefits?

Using Docker means...

  • Simple and fast: Docker provides the isolation and consistency commonly implemented with Virtual Machines (VMs), only without all the overhead.

  • Increased productivity: Docker provides a wide range of tools to manage complex application stacks.

  • Automation: Think about how much time is spent setting up installing dependencies, such as Python or Java. Or setting up configuration. Now think about how many steps are repeatable on different machines. With Docker, you can define all these within a container and automate the process of setting it all up.

How I Dockerized My Postgres Database

  • I first shut down the Postgres on my local machine with the following command 

Hello, World!

sudo su - postgres

pg_ctl -D /Library/PostgreSQL/12/data status

  • I created a docker-compose.yml file with the following configuration inside

version: "3.0"
 services:
  db:
   image: postgres
  ports:
    - "5432:5432"
  volumes:
    - ./tmp/db:/var/lib/postgresql/data
  environment:
    POSTGRES_USER: "postgres"
    POSTGRES_PASSWORD: "psql"


  • I then ran docker-compose up to create my container image

  • I ran docker-compose run web rake db:create to create my database

  • I ran docker-compose run web rake db:migrate to migrate my tables.

How I Dockerized My Rails App

  • I created Dockerfile and configured as follow

FROM ruby:2.6.3
RUN mkdir /tictactoe
WORKDIR /tictactoe
RUN apt-get update && apt-get install -y \
  curl \
  build-essential \
  libpq-dev &&\
  curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
  curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  apt-get update && apt-get install -y nodejs yarn
COPY yarn.lock ./
COPY Gemfile* ./
COPY package.json ./
COPY . .
RUN gem install bundler && bundle install
RUN bundle update rake
RUN yarn install


  • I modified my docker-compose file as follow

version: "3.0"
  services:
    db:
      image: postgres
    ports:
       - "5432:5432"
    volumes:
       - ./tmp/db:/var/lib/postgresql/data
   web:
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "psql"
      POSTGRES_HOST: "db"
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - "3000:3000"
    depends_on:
      - db      


  • I also edited my database.yml file to look like this:

default: &default
  adapter: postgresql
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    timeout: 5000
    encoding: unicode
    host: <%= ENV.fetch("POSTGRES_HOST") %>
    username: <%= ENV.fetch("POSTGRES_USER") %>
    password: <%= ENV.fetch("POSTGRES_PASSWORD") %>
development:
  <<: *default
  database: tictactoe
test:
  <<: *default
  database: tictactoe_test
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>



  • I ran docker-compose build to build my Dockerfile

  • I then ran docker-compose up to create my container images and also start my application

How I Deployed my Docker Container to Heroku

  • I created a heroku.yml file and configured as follow

setup:
  addons:
    - plan: heroku-postgresql
      as: DATABASE
build:
  config:
    RAILS_ENV: production
  docker:
    web: Dockerfile
release:
  command:
    - bundle exec rake db:migrate
  image: web

  • I added a line of code to my Dockerfile

CMD bundle exec rails server -b 0.0.0.0

  • I staged and committed my branch.

  • I ran the commands below

heroku login

heroku create tictactoe

  • I added Postgres database on my Heroku app

  • I added this line of code on my development environment in the config

  config.hosts << "julius-tictactoe.herokuapp.com"

  • I ran git push heroku ch_deploy_docker-image_to_heroku:master to deploy to heroku

You can checkout my app by clicking Tictactoe





Reference

syndritraining

Previous
Previous

My Experience with Docker

Next
Next

CORS -May 04