For myself Rails6 API mode + MySQL5.7 environment construction with Docker (docker compose)
Introduction of Docker itself is omitted
$ cd
$ mkdir sample_app
$ cd sample_app
sample_app$ touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock}
sample_app$ ls
Dockerfile docker-compose.yml Gemfile Gemfile.lock
Dockerfile
FROM ruby:2.6.5
#Install required packages (Install yarn since Rails 6 has Webpacker)
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 build-essential libpq-dev nodejs yarn
#Creating a working directory
RUN mkdir /myapp
WORKDIR /myapp
#Add the host side (local) (left side) Gemfile to the container side (right side) Gemfile
ADD ./Gemfile /myapp/Gemfile
ADD ./Gemfile.lock /myapp/Gemfile.lock
#Gemfile bundle install
RUN bundle install
ADD . /myapp
sample_app/docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: root
ports:
- "3306:3306"
web:
build: .
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'"
tty: true
stdin_open: true
depends_on:
- db
ports:
- "3001:3001"
volumes:
- .:/myapp
sample_app/Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6.0.3'
--api
option is added.--webpacker
option is added.$ docker-compose run web rails new . --force --database=mysql --skip-bundle --api --webpacker
database.yml
default: &dafault
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password # docker-compose.yml MYSQL_ROOT_PASSWORD
host: db # docker-compose.yml service name
$ docker-compose build
-d
option. Doing this will return you to the prompt screen with the container running$ docker-compose up -d
$ docker-compose up -d
$ docker ps -a
$ docker exec -it <Container ID> /bin/bash
$ rails db:create
$ rails db:migrate
$ exit
docker-compose run
command after starting the container$ docker-compose run web rails db:create
$ docker-compose run web rails db:migrate
That's all for construction.
Now opens on localhost: 3001
.
--Do not stop with Ctrl + C. The container remains and an error occurs at the next startup --If you do, delete tmp/pids/server.pid and restart with docker-compose up again
$ docker-compose down
$ docker-compose up --build
# docker-compose run {Service name} {Arbitrary command}
$ docker-compose run web bundle install
--Start if the container is not started
$ docker-compose up -d
--Check mysql id
$ docker ps
--Login to MySQL container
$ docker exec -it <MySQL container ID> /bin/bash
$ mysql -u root -p -h 0.0.0.0 -P 3306 --protocol=tcp
mysql>
//escape
mysql> quit
that's all.
Rails5 + MySQL on Docker environment construction by too polite Docker-compose (Docker for Mac) [Rails] Building an environment with Rails 6.0 x Docker x MySQL
Recommended Posts