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.
$ 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
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