[Ruby] I built a rails environment with docker and mysql

2 minute read

Created rails environment in docker https://qiita.com/NA_simple/items/5e7f95ae58eec5d20e1f

If for some reason you think that it doesn’t work, you can’t install mysql-clients. Refer to the URL below for how to rewrite. https://qiita.com/yagi_eng/items/1368fb2a234629a0c8e7

I’m addicted to it again when I’m in good condition.

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.

If you wonder, why is the ruby version different? ? 2.7.1 was not found even when I searched for the version with rbenv. rbenv Noticing the old thing, update

Update rbenv https://qiita.com/pugiemonn/items/f277440ec260b8d4fa6a

Noticed that the gem from is also old,

terminal


$ gem update

I still haven’t… Perform bundler update.

Since I was desperate here and there, I set aside time.

Once organized, puck through the steps from the beginning of a different site.

https://toranoana-lab.hatenablog.com/entry/2020/06/05/173658

Start docker and access localhost without stopping! !! !! Yoshsha! !! !! !!

localhost_3000


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

Why :D It seems that there are no files when googled, so there are a lot of articles that I am making with sudo touch. But delete and create while thinking that there is a file

Same error.

? ? ? I realized that I did not start mysql here This is it! !! !! Thinking

terminal


mysql.server start

Does not start. Apparently when googled

terminal


sudo rm mysql.sock
brew uninstall mysql
brew install mysql

Then, it seems that after deleting the sock file, uninstall mysql → install. The path to mysql.sock was known from the previous error, so I deleted it and then

terminal


mysql.server start

… passed! !! !! !!

Did this go…?

image.png

Yatter! !! !! !! !! !! !! !! !!

I think it was faster to build after understanding docker. Let’s study again. myself.

I’m glad that anything moved

Contents of each final file↓

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"]