The purpose is to start the Rails application with the minimum required implementation while building the Docker environment. In this article, you can also build a Ruby on Rails environment that supports Rails 6 with Docker. It is possible.
I've been using Rails for about half a year and Docker for about a month. I'm used to touching it, but there are some parts that I don't understand, so I would appreciate it if you could point out any inappropriate expressions.
"I touched Rails, but I don't know what to start with." "I often hear Docker, but it seems difficult and I don't know where to start." "I want to make something using Docker for the time being!"
I am the same programming beginner as me. I had a more difficult image of Docker than before, but I was able to actually work on it and dispel that image. I will try to implement it with the minimum implementation first and start the application.
First, create a working folder for developing the app with mkdir in any location. Suppose you want to create a folder named "work_space".
Use the cd command to move to the working folder.
$ cd work_space(= Created folder name)
In this working folder, save the files necessary for building the rails and Docker environment.
Create a Dockerfile in the folder created in 1-1 (the same applies to the following files). This Dockerfile contains the steps to build a Docker image.
Create the Ruby version as follows in the latest 2.7.2 (as of December 2020).
Dockerfile
FROM ruby:2.7.2-alpine3.12
RUN apk update && apk upgrade && apk add build-base \
curl \
curl-dev \
less \
linux-headers \
libc6-compat \
libxml2-dev \
libxslt-dev \
mariadb-dev \
pcre-dev \
nodejs \
ruby-dev \
tzdata \
yaml-dev \
zlib-dev
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN gem update bundler && bundle install
ADD . /app
EXPOSE 3000
CMD bundle exec rails s
I will briefly explain the contents. ** [FROM] ** Image and version to use ** [RUN] ** Command execution Install the package required to start Rails
docker-compose.yml is a file that describes each service that makes up an application and is the basis for executing them all at once. Here, "db (specify MYSQL)" and "web (specify application, port, etc.)" are described.
Dockecompose
version: '3'
services:
app:
build: .
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
ports:
- 3000:3000
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: root
ports:
- 3306:3306
volumes:
- db-store:/var/lib/mysql
volumes:
db-store:
** [version] ** This is the version of docker-compose. As of December 2020, the latest is '3'. ** [services] ** Let's create services in the hash below. This time the name is app I named it db. ** [image] ** image to use (mysql5.7 is specified in db) ** [environment] ** Setting environment variables in the container ** [volumes] ** Directory mount settings (db can be made persistent) ** [build] ** Path with Dockerfile etc. (basically current directory) ** [command] ** command (delete the server.pid file and then start the rails server) ** [ports] ** Port number. Set in [Host: Container]. ** [depends_on] ** Shows dependencies and allows you to specify the startup order. Here, start from db → web
gem 'rails', '~> 6'
Specify the version of Rails to install. Here, it is compatible with Rails 6. The contents of the Gemfile will be rewritten later by doing rails new.
Create an empty app with rails new under the working directory created earlier.
rails new sample_app
Put the Docker file and Docker compose file created earlier under the rails app I will move it. The final folder structure should look like the one below.
work_space
sample_app
.....
- Dockerfile
- Gemfile
- docker-compose.yml
.....
The Gemfile has been rewritten by rails new earlier. Since this database connection is MYSQL, edit the inside of Gemfile.
gem 'sqlite3'
↓
gem 'mysql2'
2-3.bundle install
Bundle install based on the contents of the Gemfile you edited earlier. If you execute the following command, bundle install will be done when building the Docker image.
$ docker-compose build
Rewrite the Rails config/database.yml file as follows.
rconfig/database.yml
default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
encoding: utf8mb4
username: root
password: password
host: db
development:
<<: *default
test:
<<: *default
production:
<<: *default
2-5.Docker Start the container and application with the following command.
$ docker-compose up
Check if the last container is running with the following command. If the container is running It's a success.
$ docker-compose ps
When I access http: // localhost: 3000/with a browser, Rails is launched successfully.
I built an environment for Ruby on Rails that supports Rails 6 with Docker. The goal was to launch the app with minimal implementation for those who started Docker. If you understand each process, it will not be so difficult. I hope it will be a reference article for people like me.
I referred to the following article. Thank you very much.
https://knowledge.sakura.ad.jp/13265/ https://qiita.com/daichi41/items/dfea6195cbb7b24f3419