Incorporate Docker into your Ruby on Rails portfolio!

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!

ec2-user@ip-xxx-xx-xx-xxx ~]$git clone GitHub repository URL

Build image

ec2-user@ip-xxx-xx-xx-xxx]$ cd myapp
[ec2-user@ip-xxx-xx-xx-xxx myapp]$ docker-compose build

Preparation before starting the server

[ec2-user@ip-xxx-xx-xx-xxx myapp]$ docker-compose run app rails assets:precompile RAILS_ENV=production

Preparation before starting the server

[ec2-user@ip-xxx-xx-xx-xxx myapp]$ docker-compose up -d

Create database, read migration file

[ec2-user@ip-xxx-xx-xx-xxx 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!

Recommended Posts

Incorporate Docker into your Ruby on Rails portfolio!
Portfolio creation Ruby on Rails
Incorporate Elasticsearch into your Rails app
[Portfolio] Bookmark management app [Ruby on Rails]
Beginners create portfolio in Ruby on Rails
Introducing Rspec with Ruby on Rails x Docker
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
I made a portfolio with Ruby On Rails
Docker the development environment of Ruby on Rails project
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Incorporate circleCI into CircleCI Rails app
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
Use Docker on your M1 Mac
Rails on Docker environment construction procedure
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
Deploy Rails on Docker to heroku
[Ruby on Rails] Convenient helper method
Deploy your Rails app on Heroku
[Ruby on Rails] Stop "looping until ..."
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]