Building a Rails 5.1.0 (Puma) + Nginx + MySQL Environment with Vagrant + docker-compose
Overview
I built this to make it easy to set up and develop a Rails environment.
I use it in situations like these:
- Developing a new project
- Testing a new gem or other middleware
- When I want to quickly build a mockup to show to a director
I’ve put together the setup steps below.
Environment
- macOS Sierra 10.12.5
- VirtualBox 5.1.18r114002
- Vagrant 1.9.3
- VagrantBox Ubuntu 14.04.5
- Docker version 17.06.0-ce, build 02c1d87
Git Clone
1 | macOS%$ git clone https://github.com/kenzo0107/vagrant-docker |
Creating the Rails Project
1 | // database = mysql |
puma.rb Configuration
1 | // backup |
- ./rails/config/puma.rb
1 | threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i |
Configuring Database Connection Settings
1 | // backup |
- ./rails/config/database.yml
1 | default: |
Creating the Database
1 | vagrant%$ docker-compose run --rm web rails db:create |
Running Rails
1 | vagrant%$ docker-compose up -d |
When you access http://192.168.35.101, the Rails welcome page is displayed.
rails g
When running rails g, it’s generally best to run it in a one-off container.
For example, the following creates an articles table, along with the associated controller, view, and model.
1 | vagrant%$ docker-compose run --rm web rails g scaffold article title:string body:text |
Updating the Gemfile
When you update the Gemfile, rebuild and restart.
1 | vagrant%$ docker-compose stop web |
Afterword
Connecting to the Rack server had its quirks, but once I got past that, everything went smoothly ♪
Since the DB is exposed on port 3306, you can connect to it from your local Mac with Sequel Pro to verify it.
Using this as a base, you can do things like checking logs with EFK, or testing email functionality with mailcatcher — it’s handy in all sorts of ways.
Thank you, Docker ♪
Building a Rails 5.1.0 (Puma) + Nginx + MySQL Environment with Vagrant + docker-compose
https://kenzo0107.github.io/en/2017/08/22/docker-compose-rails-nginx-mysql-on-vagrant/
