Docker Super Introduction: Kiyoto Programming University
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
Mac OS docker-compose 1.27.4 heroku/7.47.7 Mysql 8.0 ruby 2.7 rails 6.1.0
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
terminal.
rails_on_docker % heroku create <rails-koumori> // < >Specify any name inside
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
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
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>
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
From the last Rails construction to the deployment this time.
Recommended Posts