Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]

Hello. This time too, I summarized the procedure for building a Rails application development environment with Docker. Last time I started two containers for Rails and a database, but this time I also started a container for Nginx as a Web server.

Advance preparation

-Install Docker-for-mac

environment

Ruby: 2.5.8 Rails: 5.2.4.4 MySQL: 5.7.31 Nginx: 1.19.2 Docker: 19.03.12 Docker Compose: 1.26.2

procedure

1. Creating directories and files

The overall configuration is as follows. Now, let's create directories and files as shown in this configuration diagram.

Overall composition


/test-app
├── Dockerfile
├── Dockerfile.nginx
├── docker-compose.yml
├── nginx.conf
├── Gemfile
└── Gemfile.lock

First, create the root directory of your project.

terminal



$ mkdir test-app

And directly under the root directory

Create these.

terminal



$ cd test-app
$ touch Dockerfile Dockerfile.nginx docker-compose.yml nginx.conf Gemfile Gemfile.lock 

2. Edit the file

The contents of each file created above are as follows. (Leave Gemfile.lock empty.)

Dockerfile



FROM ruby:2.5
RUN apt-get update && apt-get install -y \
    build-essential \
    node.js
WORKDIR /test-app
COPY . /test-app
RUN bundle install

Dockerfile.nginx



FROM nginx
RUN rm -f /etc/nginx/conf.d/*
COPY nginx.conf /etc/nginx/conf.d/test-app.conf
CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

containers/nginx/nginx.conf



#Specify proxy destination
#Send the request received by Nginx to the backend puma
upstream test-app {
  #I want to communicate with sockets, so puma.Specify sock
  server unix:///test-app/tmp/sockets/puma.sock;
}

server {
  listen 80;
  #Specify domain or IP
  server_name _;

  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log;

  #Specifying the document root
  root /test-app/public;

  client_max_body_size 100m;
  error_page 404             /404.html;
  error_page 505 502 503 504 /500.html;
  try_files  $uri/index.html $uri @test-app;
  keepalive_timeout 5;

  #Reverse proxy related settings
  location @test-app {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://test-app;
  }

  location /favicon {
    empty_gif;
    access_log    off;
    log_not_found off;
  }
}

Gemfile



source 'https://rubygems.org'
gem 'rails', '~>5.2'

docker-compose.yml



version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec puma -C config/puma.rb
    volumes:
      - .:/test-app
    tty: true
    stdin_open: true
    depends_on:
      - db

  db:
    image: mysql:5.7
    environment:
      - 'MYSQL_ROOT_PASSWORD=password'
    volumes:
      - 'db-data:/var/lib/mysql'

  web:
    build:
      context: .
      dockerfile: Dockerfile.nginx
    volumes:
      - ./public:/test-app/public
      - ./tmp:/test-app/tmp
    ports:
      - 80:80
    depends_on:
      - app

volumes:
  db-data:

3. Set up Rails in the App container

terminal



$ docker-compose run --rm app rails new . --force --database=mysql --skip-bundle

4. Create tmp / sockets folder and edit the created file

First, create a sockets folder inside the tmp folder.

Then, edit the following three of the files created by Rails setup.

config/database.yml



default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password   #docker-compose.yml MYSQL_ROOT_Set the value of PASSWORD
  host: db   #docker-compose.Match with yml service name

development:
  <<: *default
  database: test-app_development

config/puma.rb



threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count

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

environment ENV.fetch("RAILS_ENV") { "development" }

pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

plugin :tmp_restart

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

stdout_redirect "#{app_root}/log/puma.stdout.log", "#{app_root}/log/puma.stderr.log", true

config/environments/production.rb


# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true   #The default is false, so change it to true

There is no problem in building the development environment without changing this config / environments / production.rb, but I got an asset precompile error when deploying to the production environment, so I changed it here. It is.

5. Start container, create DB

terminal



$ docker-compose up -d --build
$ docker-compose exec app rails db:create

Now when you go to http: // localhost, you should see the Rails home screen.

reference

Recommended Posts

Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
[Docker] Rails 5.2 environment construction with docker
React + Django + Nginx + MySQL environment construction with Docker
Laravel development environment construction with Docker (Mac)
Create Rails 6 + MySQL environment with Docker compose
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Rails Docker environment construction
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
[Rails] Development with MySQL
Build a development environment for Django + MySQL + nginx with Docker Compose
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
[Jakarta EE 8 application development with Gradle] 1. Environment construction
Wordpress local environment construction & development procedure with Docker
MySQL 5.7 (Docker) environment construction memo
[Rails / MySQL] Mac environment construction
React environment construction with Docker
BEAR application Docker development environment construction example (docker-sync, Mutagen)
Stable development environment construction manual for "Rails6" with "Docker-compose"
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Node.js environment construction with Docker Compose
Environment construction for Servlet application development
Environment construction with Docker for beginners
Build environment with vue.js + rails + docker
Easy environment construction of MySQL and Redis with Docker and Alfred
Build Rails environment with Docker Compose
Java web application development environment construction with VS Code (struts2)
Rails on Docker environment construction procedure
How to build Rails, Postgres, ElasticSearch development environment with Docker
Ruby on Rails --From environment construction to simple application development on WSL2
How to install Pry after building Rails development environment with Docker
PostgreSQL environment construction with Docker (from setup to just before development)
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
SQL statement learning ~ Environment construction ~ Docker + MySQL
GPU environment construction with Docker [October 2020 version]
Building Rails 6 and PostgreSQL environment with Docker
Sapper × Go (echo) × Docker development environment construction
Build a PureScript development environment with Docker
Deploy to heroku with Docker (Rails 6, MySQL)
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
Spring Boot + Docker Java development environment construction
[AWS] Publish rails app with nginx + puma
Rails & React & Webpacker & MySQL Environment Construction Manual
Lightweight PHP 7.4 development environment created with Docker
How to build Rails 6 environment with Docker
Procedure for migrating Rails application development environment to Docker even if you are inexperienced (Rails5 + MySQL8.0 + docker-compose)
Docker environment construction
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
I built a rails environment with docker and mysql, but I got stuck
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Creating a java web application development environment with docker for mac part1
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
I tried to build a Firebase application development environment with Docker in 2020
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter