LoginSignup
1
0

More than 3 years have passed since last update.

rails5のDockerfileとdocker-compose.ymlを書く

Last updated at Posted at 2019-12-26

rails6はこちら
rails6のDockerfileを書く - Qiita

Dockerfile
// 元となるイメージ
FROM ruby:2.6.5

// nodejsが必要なので入れる
RUN apt-get update -y && \
    apt-get install default-mysql-client nodejs -y && \
    gem install rails --version='5.2.4'

// GemfileとGemfile.lockは他でrails newしたときのをコピーして作成するとよい
COPY Gemfile /Gemfile
COPY Gemfile.lock /Gemfile.lock

// bundle installを実行させる
RUN bundle install
docker-compose.yml
version: '3'
services:
  mysql:
    // このイメージを元にする
    image: mysql:8.0.18
    // パスワードの認証方法を変更
    command: --default-authentication-plugin=mysql_native_password
    // password設定
    environment:
      MYSQL_ROOT_PASSWORD: xxxx
    // データを保存するための設定
    volumes:
      - "./mysql-data:/var/lib/mysql"
    // これをやらないとDBクライアントでつなげなくなる
    ports:
      - "3306:3306"
  web:
    // buildするDockerfileを指定する
    build:
      context: .
      dockerfile: Dockerfile
    // マウントする場所のマッピング
    volumes:
      - ".:/app"
    // portのマッピング
    ports:
      - "3000:3000"
    // コンテナが起動しつづける
    tty: true
    // mysqlが起動してからwebを起動させる
    depends_on:
      - mysql
    // コンテナの中に入ったときに/appにいるようにする
    working_dir: "/app"

Dockerfileとdocker-compose.ymlがあるディレクトリに移動して次のコマンドをうつ

// --buildは初回だけでよくて、うまくいったら次回はなくてもよい
docker-compose up --build

// 次でコンテナ内に入れる
docker exec -it コンテナ名 bash

// いつものようにrails new
rails new .

rails s -b 0.0.0.0

補足

// mysqlへの入り方
mysql -u root -p xxxx -h mysql
/config/database.yml

// 次を書き換えることが必要。これをしないとdb:createでこける。
  password: xxxxx
  host: mysql

error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

参考サイト

youtubeで見つけてこのシリーズを全部見ました。
必要最低限なことをシンプルに解説してくれていて、とても分かりやすかったです。

Docker超入門という動画を作りました。 - Qiita

Docker超入門 Part1 - Qiita


docker-compose up したコンテナを起動させ続ける方法 - Qiita

docker-composeでDBの起動完了を待ってからWebアプリを実行する - Qiita

1
0
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
1
0