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.
$ docker-compose up
, this error ...Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
① Install MySQL locally ②bundle install ③ Confirm that Gemfile.lock is created
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 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
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. .. ..
After building with docker-compose build --no-cache
, I did docker-compose up
,
It was no good. ..
Recommended Posts