Qiang Blog

Just another zhangjingqiang's blog.

Deploy Rails with Docker

A sample using Rails + MySQL

Install Docker on Mac OS X

http://docs.docker.com/engine/installation/mac/

Create docker machine

$ docker-machine create --driver virtualbox testapp

Import ENV

eval "$(docker-machine env testapp)"

Create Dockerfile

FROM ruby:2.2.0
RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
RUN mkdir /testapp
WORKDIR /testapp
ADD Gemfile /testapp/Gemfile
ADD Gemfile.lock /testapp/Gemfile.lock
RUN bundle install
ADD . /testapp

Create docker-compose.yml

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: 1234
  ports:
    - "3306:3306"
web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - .:/testapp
  ports:
    - "3000:3000"
  links:
    - db

Modify config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: 1234
  host: db

development:
  <<: *default
  database: testapp_development

test:
  <<: *default
  database: testapp_test

production:
  <<: *default
  database: testapp_production

Build image

$ docker-compose build

Boot app

$ docker-compose up

Create database

$ docker-compose run web rake db:create

Get docker machine's ip

$ docker-machine ip testapp

Access app

http://[IP_ADDRESS]:3000


Reference

https://docs.docker.com/compose/rails/

docker mysql ruby-on-rails