LoginSignup
0
0

More than 3 years have passed since last update.

dockerとmysqlでrails環境を構築したけどドハマリした

Last updated at Posted at 2020-06-23

dockerにrails環境を作った
https://qiita.com/NA_simple/items/5e7f95ae58eec5d20e1f

途中なぜか上手くいかないと思ったら、mysql-clientsがインストールできなくなっているらしい。書き換え方は下のURLを参考に。
https://qiita.com/yagi_eng/items/1368fb2a234629a0c8e7

調子に乗ってすすめていると、またハマる。

terminal
$ docker-compose run web rails db:create
Starting postgress_db ... done
Could not find activesupport-5.2.4.3 in any of the sources
Run `bundle install` to install missing gems.

なぜだ、、、と思ったらrubyのバージョンが違う??
rbenvでバージョンを探しても、2.7.1が見つからず。
rbenv古い事に気づき、アップデート

rbenvをアップデート
https://qiita.com/pugiemonn/items/f277440ec260b8d4fa6a

からのgemも古い事に気づき、

terminal
$ gem update

まだいかない、、、
bundler updateを行う。

ここらへんで絶望したので時間を置く。

一旦整理して、違うサイトの最初から手順をパクる。

なんと止まること無くdockerの起動、localhostにアクセス可能に!!!
よっしゃ!!!!

localhost_3000
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

なんで :D
ググるとファイルが無いらしいからsudo touchで無理やり作ってる記事がちらほら。
でもファイルあるしなあ、と思いつつ削除&作成

同じエラー。

???と思い、ここでmysql立ち上げていないことに気づく
これだ!!!と思って

terminal
mysql.server start

を実行するも、起動しない。
ググるとどうやら

terminal
sudo rm mysql.sock
brew uninstall mysql
brew install mysql

と、sockファイルを消してからmysqlをアンインストール→インストールで治るらしい。
mysql.sockのパスは前のエラーで判明していたので、それを消してから

terminal
mysql.server start

…通った!!!!

これは行ったか…?

image.png

ヤッターーーーーーー!!!!!!!!!

docker理解してから構築したほうが早かったと思います。
勉強し直しましょう。自分。

なにはともあれ動いてよかった

最終的な各ファイルの中身↓

gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
docker-compose.yml
version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD:
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
    command: bundle exec rails server -b 0.0.0.0
volumes:
  mysql-data:
    driver: local
Dockerfile
FROM ruby:2.7
RUN 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 -qq \
    && apt-get install -y nodejs yarn \
    && mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]
0
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
0
0