Skip to content

tamochia/rails5-docker-compose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dockerize a Rails5 Application

This repository gives you a Rails application (Rails 5 + MariaDB 10.3 + Puma + Nginx) development environment in Docker.

  • Ruby 2.6.5-slim
  • Rails 5.2.4
  • MariaDB 10.3
  • Nginx 1.16.1

Requirement

  • Docker Engine 18.06.0 or higher
  • Docker Compose 1.25.4 or higher

Usage

1. First, specify the absolute path of the parent directory of this git project (directory) in .env file

  • .env
HOSTSRCPATH=c:/Users/foo/devel/rails5-docker-compose

In the above example, the PATH in Docker Desktop for Windows.

2. Change the following if you want to change the Rails project name (Application Directory name).

[foo@localhost:rails5-docker-compose]$ grep -R -e 'sample_app' *
docker-compose.yml:              target: /opt/sample_app
docker-compose.yml:              target: /opt/sample_app/public
docker-compose.yml:              target: /opt/sample_app/tmp
docker-compose.yml:              target: /opt/sample_app/public
docker-compose.yml:              target: /opt/sample_app/tmp
nginx/Dockerfile:ADD nginx.conf /etc/nginx/conf.d/sample_app.conf
nginx/nginx.conf:    server unix:///opt/sample_app/tmp/sockets/puma.sock;
nginx/nginx.conf:    root /opt/sample_app/public;
rails/Dockerfile:ENV APP_ROOT /opt/sample_app

3. Change the DB username (dbmaster) and root password.

  • mariadb/sql/00_init.sql
GRANT ALL PRIVILEGES ON *.* TO 'dbmaster'@'%';
  • docker-compose.yml
:
       environment:
            MYSQL_ROOT_PASSWORD: PASSWORD
            MYSQL_USER: dbmaster
            MYSQL_PASSWORD: PASSWORD
            :

4. Run the following command for create a Rails project and update Gemfile and Gemfile.lock.

$ docker-compose run --rm app rails new . \
--force --no-deps --database=mysql \
--skip-turbolinks --skip-coffee --skip-sprockets --webpack

Asset Pipline (Sporokets) seems to be out of date. We'll use Webpacker gem. Webpacker uses Yarn. You need to install Yarn beforehand (see rails/Dockerfile) .

Directories and files are created under rails/src and Gemfile and Gemfile.lock files are updated.

5. Delete a running container (rails5-docker-compose_db_1).

$ docker-compose down
Removing rails5-docker-compose_db_1 ... done
Removing network rails5-docker-compose_default

6. Building docker images.

$ docker-compose build

7. Change items of username, password, and host in rails/src/config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: dbmaster
  password: PASSWORD
  host: db
development:
  <<: *default
  database: sample_app_development
:

8. Create and launch containers.

$ docker-compose up

9. Open another terminal window and execute rake db:create in app container.

$ cd ~/devel/rails-docker-compose
$ docker-compose exec app rake db:create
Created database 'sample_app_development'
Created database 'sample_app_test'

10. Close this termnal window.

11. Stop containers by pressing Ctrl+C in termnal where containers are launched.

:
app_1  | Use Ctrl-C to stop
^CGracefully stopping... (press Ctrl+C again to force)
Stopping rails5-docker-compose_web_1 ... done
Stopping rails5-docker-compose_app_1 ... done
Stopping rails5-docker-compose_db_1  ... done

12. Modify rails/src/config/puma.rb.

Comment out the following line.

#port        ENV.fetch("PORT") { 3000 }

Add the following lines at the end.

bind "unix:///opt/sample_app/tmp/sockets/puma.sock"
stdout_redirect "/opt/sample_app/log/puma.stdout.log",  "/opt/sample_app/log/puma.stderr.log", true

13. Restarting containers.

$ docker-compose start
Starting db  ... done
Starting app ... done
Starting web ... done

14. Try accessing the URL http://localhost/ in a web browser.

$  curl -I http://localhost/
HTTP/1.1 200 OK
Server: nginx/1.16.1
:

About

Sample Rails Application by Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published