There are various ways to launch rails with docker, but I couldn't find one that uses webpacker, Action cable, and Active storage, so I summarized them.
Gemfile First, since postgresql and redis are used as the database, describe them in the Gemfile. If you don't use action cable, you can just use postgresql.
Gemfile
gem 'redis'
gem 'pg'
database.yml Next, we will set up the connection of postgresql. We will set usernames and passwords as environment variables later.
config/database.yml
production:
adapter: postgresql
encoding: unicode
username: <%= ENV.fetch("POSTGRES_USER") %>
password: <%= ENV.fetch("POSTGRES_PASSWORD") %>
pool: 5
host: db
database: <%= ENV.fetch("DB_NAME") %>
production.rb Next, set the ssl of the production environment. Uncomment the next part. This will always set the ssl setting. Since static files are delivered by nginx, static file delivery settings are not set.
config/environments/production.rb
config.force_ssl = true
puma.rb Next, we will set up puma. Here, in order to set to communicate with socket, the PORT setting is deleted and the bottom two lines are added.
config/puma.rb
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
environment ENV.fetch("RAILS_ENV") { "development" }
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
plugin :tmp_restart
app_root = File.expand_path("../..", __FILE__)
bind "unix://#{app_root}/tmp/sockets/puma.sock"
master.key Next, set master.key. It is required along with credentials that can encrypt and share API keys and so on. You can give the API key etc. to github etc. in an encrypted state. Instead, if you do not share master.key, you will not be able to combine it and an error will occur at the time of deployment, so set it.
vim config/master.key
#Local config/master.Please copy and paste the key
sockets If the sockets directory does not exist under tmp, create it because an error may occur when starting puma unless you create it.
cd tmp
mkdir sockets
touch sockets/.keep
Please install docker and docker-compose according to your environment.
The files required to build a Rails environment with docker-compose are summarized below. https://github.com/arato-make/docker-rails
git clone https://github.com/arato-make/docker-rails.git
#Please go in the directory where Gemfile etc. exists
cd docker-rails
ln docker-compose.yml ../docker-compose.yml
ln .env ../.env
Please edit the .env file because the items that need to be changed according to your environment are summarized in the .env file.
.env
COMPOSE_PROJECT_NAME=rails
POSTGRES_USER=username(ex: postgres)
POSTGRES_PASSWORD=Password (ex: password)
DB_NAME=Database name(ex: app_production)
SERVER_NAME=server name(ex: example.com)
docker-compose build
docker-compose run app rake db:migrate
The rails part corresponds to COMPOSE_PROJECT_NAME set in the environment variable. Please set the e-mail address and domain part according to your environment.
sudo docker run --rm -p 443:443 -p 80:80 --name letsencrypt -v "rails_certs:/etc/letsencrypt" -v "rails_certs-data:/var/lib/letsencrypt" certbot/certbot certonly -n -m "mail address" -d domain--standalone --agree-tos
docker-compose up -d
You should be able to access it if you have completed the port / DNS settings.
Docker + Rails + Puma + Nginx(SSL) + MySQL
Set SSL/TLS with Ubuntu + nginx + LetsEncrypt
Build an HTTPS-enabled proxy server with Docker + Nginx + Let's Encrypt