LoginSignup
1
0

More than 1 year has passed since last update.

Dockerを使った環境構築

Last updated at Posted at 2021-05-24

Farstepさんの動画
「rails環境構築】docker + rails + mysql で環境構築(初心者でも30分で完了!)」
https://www.youtube.com/watch?v=Fq1PH0Gwi8I&t=0s
を視聴して環境構築をしました。かなり分かりやすくまとめられており、好き、結婚したいと思いました。備忘録も兼ねて投稿します。

環境セット
rails:5.2.2
ruby:2.5.9
docker
mysql:5.7

terminal
$ docker-compose run web rails new . --force --database=mysql --skip-bundle

まずは上記のように実行したら
以下のようにポート3306はもう使われていると出てきてしまったので、

terminal
ERROR: for mysql  Cannot start service mysql: Ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use

3306で何が使われているかを下記のコードで確かめる。
ruby:terminal
$ lsof -i:3306

下記のようにキルしたらうまくいった。

terminal
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mysqld    860 subaru   37u  IPv6 0xd138dc6aaa5f1933      0t0  TCP *:mysql (LISTEN)
mysqld  57427 subaru   29u  IPv4 0xd138dc6abc71bcfb      0t0  TCP localhost:mysql (LISTEN)

$ kill 57427
$ kill 860

terminal
$docker compose build

を実行すると大量に記事が読み込まれていくので、

terminal
[+] Building 115.7s (13/13) FINISHED                                    
 => [internal] load build definition from Dockerfile               0.1s
 => => transferring dockerfile: 428B                               0.0s
 => [internal] load .dockerignore                                  0.1s
 => => transferring context: 2B                                    0.0s
 => [internal] load metadata for docker.io/library/ruby:2.5        0.0s
 => [1/8] FROM docker.io/library/ruby:2.5                          0.3s
 => [internal] load build context                                  0.2s
 => => transferring context: 61.18kB                               0.1s
 => [2/8] RUN apt-get update -qq && apt-get install -y build-ess  31.8s
 => [3/8] RUN mkdir /myapp                                         0.5s
 => [4/8] WORKDIR /myapp                                           0.1s 
 => [5/8] COPY Gemfile /myapp/Gemfile                              0.1s 
 => [6/8] COPY Gemfile.lock /myapp/Gemfile.lock                    0.0s 
 => [7/8] RUN bundle install                                      80.6s
 => [8/8] COPY . /myapp                                            0.1s
 => exporting to image                                             1.9s
 => => exporting layers                                            1.8s
 => => writing image sha256:b5280eae293ce4e92073022898bd95fd538e1  0.0s
 => => naming to docker.io/library/sample_app_web                  0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

が終わったら以下を実行する。

terminal
$ docker-compose up

そうしたらまたエラーが出てきた。

terminal
web_1  | Could not find gem 'mysql2 (>= 0.4.4, < 0.6.0)' in any of the gem sources listed in your Gemfile.
web_1  | Run `bundle install` to install missing gems.
sample_app_web_1 exited with code 7

調べたところ、開発環境にmysqlが入っていなかったことが原因だったみたい。

terminal
$ brew install mysql
$ brew upgrade mysql
terminal
$ bundle install
$ gem update

をしたら

terminal
$ bundle install
Your Ruby version is 2.3.7, but your Gemfile specified 2.5.9

と出てきたので、アメブロのgoemonhanaさんの記事(めっちゃ分かりやすい求婚したい。)を読んで解決させた後、再度$bundle installを実行
ruby:terminal
$bundle install

すると下記のようなエラーが出てきたので、

terminal
An error occurred while installing mysql2 (0.5.3), and Bundler
cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source
'https://rubygems.org/'` succeeds before bundling.

下記コマンドを実行

terminal
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
$ bundle install

やっと上手くいった、、そして以下のコマンドを入力。

terminal
$docker-compose build
$docker-compose up

localhost:3000で立ち上げたが、もう使用済だったため使えず。仕方なくポートをずらして使うことに。

docker-compose.yml
version: '3'

services:
    db:
        image: mysql:5.7
        environment:
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: password
        ports:
            - "3306:3306"
        volumes:
            - ./db/mysql/volumes:/var/lib/mysql

    web:
        build: .
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'" 
3000から3001に変えた
        volumes:
            - .:/myapp
            - gem_data:/usr/local/bundle
        ports:
            - 3001:30013000から3001に変えた
        depends_on:
            - db 
        tty: true
        stdin_open: true
volumes:
  gem_data:

としたらやっと開通した!!!嬉しさのあまりガッツポーズをしてしまった!
スクリーンショット 2021-05-24 17.39.02.png

後はデータベースを作成するだけなので今日はここまでで。

まとめ

railsを立ち上げる
mysqlを起動するもしくはインストールする
dockerを立ち上げる
サーバーにコネクトする
gemの中身をアップデートする
ポートを変更する。

dockerを使った環境構築って大変なんですね、、
ライブラリってすごいや。
```

$docker-compose run web rails new . --force --database=mysql --skip-bundle
$ lsof -i:3306
$ kill 57427
$ kill 860
$ docker-compose build
$ docker-compose up
$ brew install mysql
$ brew upgrade mysql
$ bundle install
$ gem update
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
```
参考文献
https://www.youtube.com/watch?v=Fq1PH0Gwi8I&t=0s
https://offlo.in/blog/port-kill.html
https://ameblo.jp/goemonhana/entry-12558599374.html
https://stackoverflow.com/questions/28091203/mysql2-gem-in-gem-list-but-getting-project-can-not-find-gem
https://qiita.com/SAYJOY/items/dd7c8fc7a3647e7ff969
https://qiita.com/uechikohei/items/c42524818e85aa6d4ac9

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