When it comes to Docker for the first time, I often don't know what to start with, but that was exactly what I was doing ...
In this article, I will explain the method and flow of introducing Docker into a Rails application (DB: MySQL) that is under development and completed. For reference, I was able to introduce it locally in about 6 hours. I will output it firmly so as not to forget the hardship (laugh) I would appreciate it if you could point out any mistakes!
First install the Docker app on your Mac The following article is easy to understand about that! Install Docker on Mac (https://qiita.com/kurkuru/items/127fa99ef5b2f0288b81)
After installation, execute the following command in ** Terminal **!
Terminal
~ % docker run -d -p 80:80 docker/getting-started
Terminal
~ % docker -v
If the output is as follows, the installation is successful.
Docker version 20.10.0, build 7287ab3
Terminal
~ % docker-compose -v
This is also successful if docker-compose version 1.27.4, build 40524192
is output in the same way.
Next, create a Docker file to install Docker and describe the settings. It is possible with a text editor, but in my case it was smoother to input from the terminal without any errors, so I will describe that method.
First, create a Dockerfile in the ** root directory ** of the application and describe it. The root directory represents the directory directly under the application as shown in the figure below.
dock_app ----|-- app
|-- bin
|-- config
|-- db
・ ・ ・ ・ ・ ・
・ ・ ・ ・ ・ ・
|-- Gemfile
|-- Gemfile.lock
|-- package.json
|-- Rakefile
|-- README.md
|-- Dockerfile ←「Dockerfile]
|-- docker-compose.yml ←[docker-compose.yml]
The procedure is as follows.
Terminal
① ~ %cd The path of the app where you want to install Docker
② App name% vi Dockerfile
#After opening the file, press "i" to enter insert mode and write below
FROM ruby:2.6.5 #Ruby version of the app
RUN apt-get update -qq && \
apt-get install -y build-essential \
libpq-dev \
nodejs
RUN mkdir /app name
WORKDIR /app name
ADD ./Gemfile /app name/Gemfile
ADD ./Gemfile.lock /app name/Gemfile.lock
RUN gem install bundler #I get an error if I don't install bundler
RUN bundle install
ADD . /app name
#When you have completed the description, press the "esc key" to ":Save as "wq"
Terminal
① App name% vi docker-compose.yml
② docker-compose.Described below in yml
#Enter "i" and write in insert mode
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: 'password' #It works fine as a password as it is
ports:
- "4306:3306" #Settings required for connecting to Docker container and Sequelpro
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app name
ports:
- "3000:3000"
depends_on:
- db
#When you have finished writing, press the "esc key" and click ":Save as "wq"
(Text editor)
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password #If there is no password setting, you can leave it as it is
socket: /tmp/mysql.sock
host: db
development:
<<: *default
database:app name_development
Only two have been added: password: password
and host: db
.
Terminal
app name~ % docker-compose build
Let's execute the above command to create a container. * It may take some time
If the output is as follows, it is successful.
Terminal
Removing intermediate container dac250609513
---> f0ba8d685e44
Step 9/9 : ADD . /app name
---> f50cb7681119
Successfully built f50cb7681119
Successfully tagged app name_web:latest
By the way, if you are a beginner, you will see debconf: delaying package configuration, since apt-utils is not installed
in red while executing the command, but as far as I can see, is there anything you care about? It looks like output.
After creating the container, create a DB on the container.
Terminal
app name% docker-compose run web bundle exec rake db:create
With ** Rails 6 **, when you run this command
========================================
Your Yarn packages are out of date!
Please run `yarn install --check-files` to update.
========================================
To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml).
It may be output like this.
If it is output like this, edit the description of the config/webpacker.yml file
according to the error as follows.
webpacker.yml
# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
check_yarn_integrity: true
# check_yarn_integrity:Let's change true to false → check_yarn_integrity: false
I think this will solve the error, so after changing the description, let's execute the ** DB creation command ** again.
Terminal
Created database 'app name_development'
Created database 'app name_test'
#As mentioned above, creating is created...Success when done
Next, let's execute migration.
Terminal
app name% docker-compose run web bundle exec rake db:migrate
==20201222010929 Create table name: migrating ====================================
-- create_table(:table name)
-> 0.0095s
==20201222010929 Create table name: migrated (0.0096s) ===========================
#Rails db as above:If the migration is executed like when you executed migrate, it is successful.
Finally, start the container and check if the app is displayed in the browser.
Let's execute the container start command.
Terminal
app name% docker-compose up
#If it is running, the following output will be output
db_1 | 2020-12-27T06:45:52.494912Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2020-12-27T06:45:52.497330Z 0 [Note] InnoDB: Buffer pool(s) load completed at 201227 6:45:52
db_1 | 2020-12-27T06:45:52.497669Z 0 [Note] mysqld: ready for connections.
db_1 | Version: '5.7.32' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
web_1 | => Booting Puma
web_1 | => Rails 6.0.3.4 application starting in development
web_1 | => Run `rails server --help` for more startup options
web_1 | Puma starting in single mode...
web_1 | * Version 3.12.6 (ruby 2.6.5-p114), codename: Llamas in Pajamas
web_1 | * Min threads: 5, max threads: 5
web_1 | * Environment: development
web_1 | * Listening on tcp://0.0.0.0:3000
web_1 | Use Ctrl-C to stop
Execute the command, let's access the local environment when the server starts http://localhost:3000/ If you can confirm the display and behavior safely, it means that you have installed Docker in the development environment.
It took me about 6 hours to do this alone, so please take advantage of this article and proceed quickly! !!
Finally, let's connect the DB!
Open the settings page below and select ** Standard **.
Describe the following on the setting page
"Name" → Set any one "Host" → "127.0.0.1" "User name / password" → Match the one described in the yml file "Port" → "4306"
After setting the above and "connecting", the DB of the corresponding application should be created in Sequel Pro.
Also, the boat number matches the description of ports:
in docker-compose.yml
.
This completes the introduction of Docker and docker-compose into the development environment. By the way, it took me about 6 hours so far, so please refer to it and proceed quickly.
① "Cloud infrastructure to learn by touching container construction from docker basics" ② Introduce Docker with MySQL to the existing rails6 app. ③ Ruby on Rails "I want to introduce Docker to an app I made halfway" (MySQL / Sequel Pro)
Recommended Posts