Catalogue
Building Rails 5 (Puma) + Nginx + MySQL with docker-compose on Vagrant (Ubuntu)

Building Rails 5 (Puma) + Nginx + MySQL with docker-compose on Vagrant (Ubuntu)

🌐 日本語で読む

This is a docker-compose setup I use as a template for my own Rails development environment.

Here are the setup steps.

Starting Vagrant

1
2
3
4
macOS%$ git clone https://github.com/kenzo0107/vagrant-docker
macOS%$ cd ./vagrant-docker/
macOS%$ vagrant up
macOS%$ vagrant ssh

Creating the Rails Project

1
2
3
// on vagrant
vagrant%$ cd /vagrant/rails-puma-nginx-mysql
vagrant%$ docker-compose run --rm web rails new . --force --database=mysql --skip-bundle

Setting the Puma Configuration File

1
vagrant%$ cp puma.rb ./rails/config/
  • ./rails/config/puma.rb
1
2
3
4
5
6
7
8
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
port ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
plugin :tmp_restart

app_root = File.expand_path("../..", __FILE__)
bind "unix://#{app_root}/tmp/sockets/puma.sock"

Setting the Database Configuration File

1
vagrant%$ cp database.yml ./rails/config/
  • ./rails/config/database.yml
1
2
3
4
5
6
7
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> # <--- MYSQL_ROOT_PASSWORD
host: db # <--- service name

Creating the Database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vagrant%$ docker-compose run --rm web rails db:create
Created database 'app_development'
Created database 'app_test'

vagrant%$ docker-compose exec db mysql -u root -p -e'show databases;'
Enter password: (password)
+--------------------+
| Database |
+--------------------+
| information_schema |
| app_development | <--- add !
| app_test | <--- add !
| mysql |
| performance_schema |
| sys |
+--------------------+

With that, we have prepared the bare minimum Rails project!

Starting All Containers: Rails, Nginx, MySQL

1
vagrant%$ docker-compose up -d

Access http://192.168.35.101 from your browser, and you can confirm that the Rails top page is displayed.

Conclusion

Because everything is containerized with Docker, it is convenient to be able to easily check the look and feel or verify functionality just by swapping out a container, even when you want to upgrade the versions of Nginx, MySQL, and so on.

By adding Elasticsearch + Kibana to visualize logs, or Mailcatcher to confirm email sending, you can prepare an environment that is more than sufficient for development.

I hope this can be of help to your development.

Building Rails 5 (Puma) + Nginx + MySQL with docker-compose on Vagrant (Ubuntu)

https://kenzo0107.github.io/en/2017/09/13/docker-compose-rails5-nginx-mysql-on-vagrant/

Author

Kenzo Tanaka

Posted on

2017-09-13

Licensed under