[Ruby] Incorporate Docker into your Ruby on Rails portfolio!

2 minute read

Current state

Hello everyone, this is my first post on Qiita!
I think there is something wrong, but please kindly tell me at that time (laugh)
This time, we are deploying a Rails portfolio that has already been created using Aws and Docker.
In the current state, I created a portfolio using rails and Aws, but I think it is difficult to build an environment, and I would like to deploy the portfolio using Docker, which makes it easy to manage the environment with code.

Diagram

The configuration diagram at the time of deployment is as follows.
スクリーンショット 2020-07-14 21.27.20.png
To explain it in a very simple way, when a communication request comes from the client, it goes to Nginx and communicates with puma when dynamic processing is required, and at that time puma and mysql also communicate.
This time I would like to create this configuration using Docker.

What you need

When deploying this time, we will use EC2 and RDS of Aws.
We will build the environment with Docker, but from the viewpoint of data persistence, we will use RDS Mysql instead of Docker this time.
The following are the preparations and work required for the work.

-Make necessary preparations when deploying using EC2 and RDS on AWS (network configuration, etc.)
-Install Dokcer in local environment and Ec2
-Install Docker-compose in local environment and Ec2

Referenced articles

-Introduce Docker to Rails application on EC2 (Rails, Nginx, RDS)
-Since AWS deployment with Rails On Docker has been completed, I will organize the contents.

work

First of all, create a Docker file.
Here, we will write the code to build the environment around Ruby using Dockerfile.

FROM ruby:2.5.7

RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev \
                       nodejs \
                       vim

RUN mkdir /App name

WORKDIR /App name

ADD Gemfile /App name/Gemfile
ADD Gemfile.lock /App name/Gemfile.lock

RUN gem install bundler
RUN bundle install

ADD . /App name

RUN mkdir -p tmp/sockets
RUN mkdir -p tmp/pids

Next, create Docker-compose.
Here, we will manage the Docker container and specify the part to be cloaked!

Docker-compose.yml


version: '3'
services:
  app:
    build: .
    command: bundle exec puma -C config/puma.rb -e production
    volumes:
      - .:/App name:cached
      - public-data:/App name/public
      - tmp-data:/App name/tmp
      - log-data:/App name/log

  web:
    build:
      context: containers/nginx
    volumes:
      - public-data:/App name/public
      - tmp-data:/App name/tmp
    ports:
      - 80:80
      
volumes:
  public-data:
  tmp-data:
  log-data:

Next, create the following files.
Here, we will set up the Nginx container.

FROM nginx:1.15.8

RUN rm -f /etc/nginx/conf.d/*

ADD nginx.conf /etc/nginx/conf.d/App name.conf

CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

containers/nginx/nginx.conf


upstream FashionInformation_app {
  server unix:///App name/tmp/sockets/puma.sock;
}

server {
  listen 8000;
  server_name domain name;

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

  root /App name/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 @App name;
  keepalive_timeout 5;

  location @App name{
    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://App name;
  }
}

Deploy a Rails application

Clone git to EC2 when you get here!

[email protected] ~]$git clone GitHub repository URL

Build image

[email protected]]$ cd myapp
[[email protected] myapp]$ docker-compose build

Preparation before starting the server

[[email protected] myapp]$ docker-compose run app rails assets:precompile RAILS_ENV=production

Preparation before starting the server

[[email protected] myapp]$ docker-compose up -d

Create database, read migration file

[[email protected] myapp]$ docker-compose exec app rails db:create db:migrate RAILS_ENV=production

If you access the public IP and it is displayed correctly, you are successful!