こんにちは
今回はモダンな開発企業さんでは必ず取り入れているとの噂の
Docker
の導入方法について紹介していきたいと思います
(自分へのメモ要素7割)
まず、Dockerとはについて解説をしたいのですが、
まだDockerを一度しか扱ったことがない僕が語るのも何なので、
Dockerとはを知るにはDockerとはどういったものなのか、めちゃくちゃ丁寧に説明してみるというこの記事がおすすめです。
サーバーに関する知識やDockerが使われるに至った歴史がわかります。
ではまずは
dockerfile
を作成してこんな漢字
FROM ruby:2.6 //バージョン指定 コマンドruby -v で確認
RUN apt-get update -qq && \
apt-get install -y build-essential \
libpq-dev \
nodejs
RUN mkdir /Books アプリ名
ENV APP_ROOT /Books
WORKDIR $APP_ROOT
ADD ./Gemfile $APP_ROOT/Gemfile
ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock
RUN gem install bundler
RUN bundle install
ADD . $APP_ROOT
webpacker.yml
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
check_yarn_integrity: false //*注意
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: false
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
check_yarn_integrity: false //*注意
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: example
# socket: /tmp/mysql.sock
host: db
development:
<<: *default
database: Books_development ご自身のデータベース名(以下同じく)
test:
<<: *default
database: Books_test
production:
<<: *default
database: Books_production
username: Books
password: <%= ENV['BOOKS_DATABASE_PASSWORD'] %>
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/lib/mysql/mysql.sock
docker-compose.yml
version: '3'
services:
db:
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example //僕の場合は設定していなかったのでテキトーに
image: mysql
ports:
- "4306:3306"
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/Books
ports:
- "3000:3000"
depends_on:
- db
*注意マークに関しては最後に紹介します。
書き終えたら、、
% docker-compose build
しばらくミルク多めコーヒーミルクを飲みながら待つ。
できたら
% docker-compose run web bundle exec rake db:create
% docker-compose run web bundle exec rake db:migrate
問題なくできたら、
% docker-compose up
仕上げにコンテナを立ち上げ。
お終い。
最後に、立ち上げる際にもはやお決まりかのごとく現れるエラーに関して紹介します。
web_1 | => Booting Puma
web_1 | => Rails 6.0.3.2 application starting in development
web_1 | => Run `rails server --help` for more startup options
web_1 | sh: 1: yarn: not found
web_1 |
web_1 |
web_1 | ========================================
web_1 | Your Yarn packages are out of date!
web_1 | Please run `yarn install --check-files` to update.
web_1 | ========================================
web_1 |
web_1 |
web_1 | To disable this check, please change `check_yarn_integrity`
web_1 | to `false` in your webpacker config file (config/webpacker.yml).
web_1 |
web_1 |
web_1 |
web_1 |
web_1 |
web_1 | Exiting
books_web_1 exited with code 1
こちらのエラー分を読んでいただければ分かるとおり、
落ち着いて、
% yarn install --check-files
or
% yarn update
または
先ほど紹介した。
*注意 マークが有るところをしっかりとfalseにする
(JavaScript パッケージが最新であるか確認してくれる記述らしいが今はいらないのでfalseにする。)
これらをすれば治ると思います。
全くの初心者なためDockerについてはこれから重ねて勉強してつもりです。
今回は
以上です。
参考記事
(というかほぼ同じ)
既存のrails6のアプリにMySQLでDockerを導入する。
Docker入門