LoginSignup
0
1

More than 3 years have passed since last update.

docker-composeでアプリサーバーコンテナからMySQLサーバーコンテナに接続する。

Last updated at Posted at 2020-04-29

実行環境

Docker for Mac
Docker version 19.03.8

簡単な事だけどかなり手こずったのでまとめ。

Dockerfile

FROM ruby:2.7.1-buster

RUN gem install rails

# node.jsをインストール
RUN apt-get update && \
    apt-get install -y node.js

# yarnをインストール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && apt-get install -y yarn
RUN rails webpacker:install
RUN yarn install --check-files

COPY Gemfile /Gemfile
COPY Gemfile.lock /Gemfile.lock

RUN bundle install

*開発環境には node.js、 yarn、 webpackerのインストールが必要なので先にインストールしておく。

docker-compose.yml

version: "3"
services:
  mysql:
    image: mysql:8.0.20
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
  app:
    build: .
    volumes:
      - ".:/app"
    ports:
      - "3000:3000"
    tty: true
    depends_on:
      - mysql
    working_dir: "/app"

これらを元にdocker-composeでコンテナを立ち上げていきます。

$ docker-compose up

立ち上がったらコンテナ内にアクセス。

$ docker exec -it (立ち上げたコンテナのNAMES) /bin/bash

mariadb-clientをインストール

$ apt install mariadb-client

mysqlにアクセス

$ mysql -u root -proot -h mysql

* docker-compose.yml参照
-u = ユーザーを指定
-p = 指定したユーザーのパスワード
-h = ホスト、mysqlコンテナの名前?
MySQL [(none)]>

*アクセス成功

一度ログアウトしconfig/database.ymlを編集します。
具体的にはdatabaseがsqliteになっていたのでmysql2に変更します。

# SQLite. Versions 3.8.0 and up are supported.
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: mysql2
  encording: utf8
  username: root
  password: root
  host: mysql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: rails_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: rails_test

production:
  <<: *default
  database: rails_production

Gemfileもsqliteからmysqlに変更

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

gem 'rails', '~> 6.0.2', '>= 6.0.2.2'
# sqliteからmysql2へ
gem 'mysql2'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'

~省略~
$ rake db:create
$ mysql -u root -proot -h mysql * もう一度mysqlに接続
$ show databases; * rake db:createが出来ているか確認
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| rails_development  |
| rails_test         |
| sys                |
+--------------------+
6 rows in set (0.011 sec)

一応ここまでやっとけば開発できると思う。完全に自分用で参考にならないかもしれません。
参考: Docker超入門 Part03 - 複数コンテナを動作させる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1