The author, who has no practical experience with Docker at all, summarized the process of migrating the development environment of his own Rails application to Docker.
The period until the transition was about a week including basic learning (if there were no errors, it could have been done in about half ...)
I hope it will be helpful for those who want to touch Docker and are stuck with errors.
**※Caution! ** ** MySQL has different default authentication formats for 5.0 series and 8.0 series. (As of August 18, 2020) As a result, the description of the file (docker-compose.yml) required to create Docker differs depending on the version **. ** ** Therefore, please make sure that the version of MySQL you are using matches the version of MySQL described in the literature.
Teaching materials: Introduction docker
Introduction Docker is a hands-on free site where you can easily learn terms and structures related to Docker.
If you don't know anything about Docker, even if you do all the content, it will take about half a day to a day, so study roughly at Introduction Docker. recommend.
The following three files are added to the existing application and need to be modified.
The file structure is as follows.
sampleApp ---- Dockerfile #Files to add
|-- docker-compose.yml #Files to add
|-- Gemfile
|-- Gemfile.lock
|-- README.md
|-- Rakefile
|-- app
|-- bin
|-- config ---- application.rb
| |-- boot.rb
| |-- cable.yml
| |-- credentials.yml.enc
| |-- database.yml #File to modify
| |-- environment.rb
| |-- environments
| |-- initializers
| |-- locales
| |-- master.key
| |-- puma.rb
| |-- routes.rb
| |-- spring.rb
| |-- storage.yml
|
|-- config.ru
|-- db
|-- lib
|-- log
|-- package.json
|-- public
|-- storage
|-- test
|-- tmp
|-- vendor
Dockerfile
Dockerfile
#Specify the Docker Image. Specify the version of the app you are using.
FROM ruby:2.5.1
#Installation of required packages. node.Regarding js, an error occurred in the original description, so it was corrected.
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client --no-install-recommends && rm -rf /var/lib/apt/lists/*
#Create and set working directory
RUN mkdir /workdir
WORKDIR /workdir
#Created the host side (local) Gemfile above/Add to workdir
ADD Gemfile /workdir/Gemfile
ADD Gemfile.lock /workdir/Gemfile.lock
#Gemfile bundle install
#I got an error when I ran it without ENV. BUNDLER_Avoided by specifying VERSION.
ENV BUNDLER_VERSION 2.1.4
RUN gem install bundler
RUN bundle install
#All directories on the host side (local) of the Docker container/Added under workdir.
ADD . /workdir
docker-compose.yml
docker-compose.yml
# docker-Specifies the compose version. This time'3'use.
version: '3'
#Define the container to start. Db in this file, db-test ,It defines three of the web.
services:
db:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password #The default authentication format is mysql_native_Change to password. MySQL5.Not required for 0 series.
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: appname_development
MYSQL_USER: yuki #Any user
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
volumes:
- ./mysql/mysql_data:/var/lib/mysql
- ./logs:/var/log/mysql
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
ports:
- "4306:3306"
db-test:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: appname_test
MYSQL_USER: yuki
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
volumes:
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
tmpfs:
- /var/lib/mysql
- /var/log/mysql
web:
build:
context: .
dockerfile: Dockerfile
command: /bin/sh -c "rm -f /workdir/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
tty: true
stdin_open: true
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/workdir
database.yml
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: appname_development
username: yuki
password: password
host: db
socket: /tmp/mysql.sock
test:
<<: *default
database: appname_test
host: db-test
username: yuki
password: password
socket: /tmp/mysql.sock
production:
<<: *default
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOSTNAME'] %>
python
$ docker-compose build #Build container
$ docker-compose up -d #Simultaneous start of container
$ docker-compose run web bundle exec rake db:create #DB creation
$ docker-compose run web bundle exec rake db:migrate #migration
If you can do this, you should be able to access it at http: // localhost: 3000.
Recommended Posts