Rails deploy with Docker

Reference site

Docker Super Introduction: Kiyoto Programming University

reference

Rails environment construction with Docker We are building a Rails environment using Docker here.

Docker Terms and Commands Learn more about Docker terms and commands

environment

Mac OS docker-compose 1.27.4 heroku/7.47.7 Mysql 8.0 ruby 2.7 rails 6.1.0

heroku implementation

heroku login

terminal.


rails_on_docker % heroku login
heroku: Press any key to open up the browser to login or q to exit:  //Login with Enter
rails_on_docker % heroku container:login

heroku app creation

terminal.


rails_on_docker % heroku create <rails-koumori>  // < >Specify any name inside

Database addition / setting

Add database

terminal.


rails_on_docker % heroku addons:create cleardb:ignite -a rails-koumori 

Modify the connection destination information of the production environment to an environment variable

database.yml


production:
  <<: *default
  database: <%= ENV['APP_DATABASE_DATABASE'] %>
  username: <%= ENV['APP_DATABASE_USERNAME'] %>
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
  host: <%= ENV['APP_DATABASE_HOST'] %>

Set connection destination information in environment variable

terminal.


rails_on_docker % heroku config -a rails-koumori
CLEARDB_DATABASE_URL: mysql://username:password@hostname/Database name?reconnect=true

rails_on_docker % heroku config:add APP_DATABASE='Database name'-a rails-koumori
rails_on_docker % heroku config:add APP_USERNAME='username' -a rails-koumori
rails_on_docker % heroku config:add APP_PASSWORD='password' -a rails-koumori
rails_on_docker % heroku config:add APP_HOST='hostname' -a rails-koumori

rails_on_docker % heroku config -a rails-koumori  //Confirmation of registration

Description for production environment

Create an empty file

terminal.


rails_on_docker % touch start.sh  

start.sh


#!/bin/sh

#Production environment
if [ "${RAILS_ENV}" ="production" ]
then
    bundle exec rails assets:precompile
fi

bundle exec rails s -p ${PORT:-3000} -b 0.0.0.0

Addition of Dockerfile

Dockerfile


#Specifying the base image
FROM ruby:2.7

#Postscript
ENV RAILS_ENV=production  
.
.
.
.

#Copy to docker
COPY start.sh /start.sh  
#Grant execute permission
RUN shmod 744 /start.sh  
#Run at startup
CMD ["sh","/start.sh"]   

Applies to production environments

terminal.


rails_on_docker % heroku config:add RAILS_SERVE_STATIC_FILES="true" -a r
ails-koumori  //Assets in production environment:Apply precompile

rails view screen implementation

Controller creation

terminal.


rails_on_docker % docker-compose exec web bundle exec rails g controller users 

Specify users/index on the top page

routes.rb


Rails.application.routes.draw do
  get '/',to: "users#index"
end

Controller description

rb:users.controller.rb


class UsersController < ApplicationController
  def index
  end
end

Create and describe index.html.erb file

html:src>app>views>users>index.html.erb


<h1>Hello world!</h1>

Push Docker container and release to heroku

Build a Docker image and push it to a container

terminal.


rails_on_docker % heroku container:push web -a rails-koumori

Release container on heroku

terminal.


rails_on_docker % heroku container:release web -a rials-koumori

Start heroku and check with a browser

terminal.


rails_on_docker % heroku open -a rails-koumori

スクリーンショット 2021-01-10 18.13.47.png

Summary

From the last Rails construction to the deployment this time.

Recommended Posts

Rails deploy with Docker
Deploy to heroku with Docker (Rails 6, MySQL)
Deploy with EC2 / Docker / Laravel
Run Rails whenever with docker
[Docker] Rails 5.2 environment construction with docker
Deploy Line bot with rails5 + Docker + Heroku Note
[Docker] Use whenever with Docker + Rails
Deploy a Docker application with Greengrass
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Deploy Rails on Docker to heroku
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
heroku: docker: deploy
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Deploy your application with VPC + EC2 + Docker.
How to build Rails 6 environment with Docker
Rails Docker environment construction
Docker x Rails 6 (memo)
Launch MariaDB with Docker
Run Pico with docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
[Rails 6] RuntimeError with $ rails s
Explode Docker with WSL2
Use Puphpeteer with Docker
Handle devise with Rails
[Rails] Learning with Rails tutorial
Operate Emby with Docker
Deploy RAILS on EC2
Notes on building Rails6 / PostgreSQL with Docker Compose
Try WildFly with Docker
Deploy Rails to ECS Fargate with AWS Copilot
Use ngrok with Docker
[Rails] Test with RSpec
Run Payara with Docker
[Docker] Connection with MySQL
Php settings with Docker
[Rails] Development with MySQL
Getting Started with Docker
Supports multilingualization with Rails!
[Rails] How to build an environment with Docker
Disposable PHP with Docker
Double polymorphic with Rails
Install Composer with Docker
Easy to display hello world with Rails + Docker
Bundle install with docker + rails6 does not reflect gem
Error encountered with notes when deploying docker on rails
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker + Rails] How to deal with Rails server startup failure
Pytorch execution environment with Docker
Use GDAL with Python with Docker
[Rails 6] Dockerize existing Rails apps [Docker]
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
Run TAO Core with Docker
[Rails] Upload videos with Rails (ActiveStorage)