Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose

Rails5 + MySQL on Docker environment construction by too polite Docker-compose (Docker for Mac) It is almost the same content as. So if you want to know more, please go there. It's a little different, such as different versions of mysql and using top-level volumes.

environment

$ mkdir rails_docker
$ cd rails_docker
$ vi Dockerfile

Dockerfile


FROM ruby:2.7.1

RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       libpq-dev \
                       nodejs \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir /recruit_web
ENV APP_ROOT /recruit_web
WORKDIR $APP_ROOT

ADD ./Gemfile $APP_ROOT/Gemfile
ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock

RUN bundle install
ADD . $APP_ROOT
$ vi Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails',  '~> 5.2.4', '>= 5.2.4.4'
$ touch Gemfile.lock
$ vi docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:8.0.21
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - rails_docker_network
    environment:
        MYSQL_DATABASE: root
        MYSQL_ROOT_PASSWORD: password
    # mysql8.0 authentication plugin(caching_sha2_password)Mysql_native_Change to password
    command: --default-authentication-plugin=mysql_native_password
    container_name: rails_db_container
  web:
    build: .
    depends_on:
      - db
    command: rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/recruit_web
    networks:
      - rails_docker_network
    ports:
      - "3000:3000"
    container_name: rails_web_container

volumes:
  db_data:

networks:
  rails_docker_network:
    name: rails_docker_network
$ docker-compose run web rails new . --force --database=mysql --skip-bundle
$ vi /confing/database.yml

database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root #add to
  password: password #add to
  host: db #add to
$ docker-compose build
$ docker-compose up -d

If you execute the following command, a database will be created and you can access it on localhost: 3000.

$ docker-compose run web rails db:create

Where it gets stuck

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:8.0.21
    volumes:
      - db_data:/var/lib/mysql
    environment:
        MYSQL_DATABASE: root
        MYSQL_ROOT_PASSWORD: password
    #I got an error if I specified the following
    ports:
      - "3306:3306 

  web:
    build: .
    command: rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app_sample
    ports:
      - "3000:3000"
    #I got an error if I specified the following(not recommended+I didn't need it because the name would be resolved without it)
    links:
      - db

volumes:
  db_data:

For some reason, if you specify the public ports of links and db

Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.19.0.4' (using password: YES)

I was angry and stumbled considerably. .. ..

For the time being, I realized that I didn't need the port and deleted it. It seems that links can be resolved even if it is not deprecated from the document, so delete it https://docs.docker.com/compose/networking/

I would appreciate if you could improve anything.

Recommended Posts

Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Rails6 [API mode] + MySQL5.7 environment construction with Docker
[Docker] Rails 5.2 environment construction with docker
[Rails / MySQL] Mac environment construction
Stable development environment construction manual for "Rails6" with "Docker-compose"
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Create Rails 6 + MySQL environment with Docker compose
Rails API server environment construction using docker-compose
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Rails & React & Webpacker & MySQL Environment Construction Manual
React + Django + Nginx + MySQL environment construction with Docker
Error deploying rails5 + Mysql to heroku with Docker-compose
Rails Docker environment construction
[Rails] Development with MySQL
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Build Rails (API) x MySQL x Nuxt.js environment with Docker
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Rails engineer environment construction ruby2.7.1
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
MySQL 5.7 (Docker) environment construction memo
React environment construction with Docker
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Case that gave up environment construction of react with ubuntu
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Ruby on Rails 6.0 environment construction memo
Rails on Docker environment construction procedure
Create Laravel environment with Docker (docker-compose)
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Troublesome Rails environment construction flow [Windows 10]
[Environment construction] Uninstall rails from local
[Rails] AWS EC2 instance environment construction
[Rails & Docker & MySQL environment construction] Could not find gem ‘mysql2 (> = 0.4.4, <0.6.0)’ in any of the gem sources listed in your Gemfile.
I built a rails environment with docker and mysql, but I got stuck
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Build a hot reload development environment with Docker-compose using Realize of Go
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
How to solve the local environment construction of Ruby on Rails (MAC)!
SQL statement learning ~ Environment construction ~ Docker + MySQL
GPU environment construction with Docker [October 2020 version]
Building Rails 6 and PostgreSQL environment with Docker
Laravel development environment construction with Docker (Mac)
Build and manage RStudio environment with Docker-compose
Try running MySql and Blazor with docker-compose
Deploy to heroku with Docker (Rails 6, MySQL)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment