If you build a Rails application on a Dcoker container and use Circleci to do automatic deployment at once Now that I've entered the labyrinth, I'm going to return to the origin. (2nd)
Ruby : 2.6.6 rails : 6.0.3.2 git : 2.23.0 heroku-cli : 7.42.13 darwin-x64 node-v12.16.2 Docker : 19.03.12
Development environment is MySQL I will try to build a production environment with a pattern called PostgreSQL.
First, create a directory to create the application, and prepare various necessary items with the touch command there.
terminal
$ touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,entrypoint.sh}
Dockerfile
FROM ruby:2.6
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update -qq \
&& apt-get install -y nodejs yarn \
&& mkdir /heroku_app
WORKDIR /heroku_app
COPY Gemfile //Gemfile
COPY Gemfile.lock /heroku_app/Gemfile.lock
RUN bundle install
COPY . /heroku_app
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3006
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- '3306:3306'
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql-data:/var/lib/mysql:cached
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3006 -b '0.0.0.0'"
volumes:
- .:/heroku_app
ports:
- "3006:3000"
depends_on:
- db
stdin_open: true
tty: true
command: bundle exec rails server -b 0.0.0.0
volumes:
mysql-data:
driver: local
Gemfile
source 'https://rubygems.org'
rails ‘6.0.3’
entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /heroku_app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
If you can prepare the above 5 points, execute the following command to create a Rails application.
terminal
$docker-compose run web rails new . --force --no-deps --database=mysql
At the same time, make a textual content.
$ docker-compose run web rails g scaffold blog title:string body:text
$ docker-compose run web rails db:migrate
$ docker-compose up -d
Next, prepare postgreSQL for your production environment before deploying your Rails app to heroku. --config / database.yml settings --Add pg to Gemfile --Settings in config / environments / deviropment.rb
config/database.yml
production:
<<: *default
adapter: postgresql
encoding: unicode
pool: 5
Prepare the gem file for the production environment.
Add pg to the production group.
Also, move MySQL into `group: development,: test do`
so that it will be treated as a development environment.
Gemfile
group :production do
gem 'pg', '~> 0.19.0'
end
It is peculiar to Rails6, but it seems that it has protection from DNS detachment binding attacks, You need to put in the host.
config/enviroments/deviropment.rb
config.hosts << "radiant-springs-45017.herokuapp.com"
I referred to the following article. https://qiita.com/kodai_0122/items/67c6d390f18698950440
After editing, build.
terminal
docker-compose build
$docker-compose run web rails db:create
$docker-compose up -d
After that, type a command and deploy to Runrun and heroku.
$ docker-compose down #If you do not drop it once, an error may occur.
$ heroku login
$heroku create app name or blank
$ heroku container:login
$ heroku container:push web
$ heroku adding:create heroku-postgresql:hobby-dev
$ heroku container:release web
$ heroku open
the end!
I omitted it, but an error occurred when deploying to heroku.
In that case, I typed $ heroku logs --tail
etc. on the terminal to check the error and solve it.
Even if you refer to a successful article, an error may occur due to differences in the environment, so I thought it was important to get into the habit of checking each time for understanding.
Recommended Posts