[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?

Overview

I'm in the middle of creating a portfolio. Since I stumbled immediately in the first environment construction, I will write it including the meaning of the memorandum. I would be happy if it is delivered to beginners who have stumbled in the same place.

Flow so far

Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.

Solution

① Install MySQL locally ②bundle install ③ Confirm that Gemfile.lock is created

Cause

The root cause was that I used to pollute my environment when I installed MySQL locally. There is a problem with the socket and permissions, and it seems that bundle install did not pull MySQL well.

Therefore, delete all the MySQL directories and install MySQL locally again. After that, it is in the form of bundle install.

Gemfile.lock wasn't working well, so docker-compose up didn't pull mysql. By the way, bundle install didn't create gemfile.lock. (Crying)

Rails × docker says that such a local state can affect. I learned a lot.

The following is the environment / background.

Created file

The Dockerfile and docker-compose.yml look like this.

Dockerfile


#Use Ruby
FROM ruby:2.6.6

#UTF character code-Set to 8
ENV LANG C.UTF-8

#Set Time Zone to Japan
ENV TZ Asia/Tokyo

#Create an app directory directly under the root
RUN mkdir /app

#In the working directory/Move by specifying app
WORKDIR /app

#Share Host Gemfile
COPY Gemfile /app/Gemfile

#Install various packages
#libpq:Required to deploy MySQL with Gem
#sudo:Authority change management
#bundler(Mechanism to install the package described in Gemfile)Install
RUN apt-get update -qq && apt-get install -y build-essential \
	libpq-dev \
	sudo \
	&& gem install bundler:2.0.1

#Install the package described in Gemfile
#Rails 6 in Gemfile.0.3'Is listed
RUN bundle install

#/Share current directory with app
COPY . /app

(It's hard to see because it's full of comments ...)

docker-compose.yml


version: "3"

#Create db and backend containers
services:
  db:
    container_name: db
    #Pull the image uploaded to Docker Hub
    image: mysql:latest
    #Environment variable settings
    environment: 
      TZ: Asia/Tokyo #Set TimeZone as well as Dockerfile
      MYSQL_DATABASE: linpy_database #Set database name
      MYSQL_ROOT_PASSWORD: ***** #Set password for root user
      MYSQL_USER: ***** #Define user
      MYSQL_PASSWORD: ***** #Set the password of the above user
    #Specify the path of the configuration file to mount
    volumes:
      - mysql_data:/var/lib/mysql
    #Set port
    ports:
      - 3307:3306
    #Set IP address
    networks:
      app_net: #App described later_Set to use the following IPs in the net network space
        ipv4_address: '172.20.0.2'

  backend:
    container_name: backend
    #Execute ComposeFile and specify the path when it is built
    build: ./backend/
    image: backend
    #Run gem to start the server, 0.0.0.Bind to 0 for full access
    command: bundle exec rails server -b 0.0.0.0
    #Always start the container
    tty: true
    #Launch input interface docker run-synonymous with it of it
    stdin_open: true
    
    #Backend is cache, temp, log,Mount on git
    volumes:
      - ./backend:/app:cached
      - bundle_data:/usr/local/bundle:cached
      - /app/vendor
      - /app/tmp
      - /app/log
      - /app/.git
    environment:
      TZ: Asia/Tokyo
    
    #Control startup order,After db
    depends_on:
      - db
    ports:
      - 5000:3000 #Port forwarding
    networks:
      app_net:
        ipv4_address: '172.20.0.3'


#Set up your own network
networks:
  #app_Define a network space called net
  app_net:
    driver: bridge #bridge Connect to network
    ipam: #IP settings
      driver: default
      config:
        - subnet: 172.20.0.0/24 #Define Subnet

#Define two volumes
volumes:
  mysql_data:
  bundle_data:

Gemfile


source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.6'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]


That's why, but there is a gem that is missing ...? I'm doing bundle install ... This came out when I did docker-compose build.

Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions

The method that could not be solved

NG1: I will add magic words and stuff

Add the following before RUN bundle install in the Dockerfile. Magic word: bundle config --local build.mysql2" --with-ldflags = -L / usr / local / opt / openssl / lib "\ It was no good. .. ..

NG2: Try clearing the cache

After building with docker-compose build --no-cache, I did docker-compose up, It was no good. ..

Recommended Posts

[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
I built a rails environment with docker and mysql, but I got stuck
Rails Docker environment construction
Rails6 [API mode] + MySQL5.7 environment construction with Docker
[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.
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
MySQL 5.7 (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
[Rails / MySQL] Mac environment construction
I can't find the docker image after updating to docker desktop 2.4.0.0
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails on Docker environment construction procedure
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
SQL statement learning ~ Environment construction ~ Docker + MySQL
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails & React & Webpacker & MySQL Environment Construction Manual
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
React + Django + Nginx + MySQL environment construction with Docker
Docker environment construction
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
I get Mysql2 :: Error :: ConnectionError in the production environment
Docker the development environment of Ruby on Rails project
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Note that I stumbled upon building the Rails environment
SSL in the local environment of Docker / Rails / puma
Build Rails (API) x MySQL x Nuxt.js environment with Docker
I can't get out of the Rails dbconsole screen
When I started ansible + docker now, I stumbled from the beginning, but I managed to start it
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Easy environment construction of MySQL and Redis with Docker and Alfred
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I did Docker construction of rails + postgresql as official, but it didn't work, so I fixed some
[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
I tried node-jt400 (Environment construction)
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
I tried the Docker tutorial!
Redmine (Docker) environment construction memo
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Until you build the docker environment and start / stop the Ubuntu container