I had a hard time with meta meta, so I will leave it as a memo Also, docker's Official is the strongest There are many parts that I do not fully understand, so I would appreciate it if you could tell me any descriptions that should be corrected. rails 6 MySQL
As it is
Move to the directory generated above in the terminal
Terminal
% touch Dockerfile
Dockerfile
FROM ruby:2.6.5
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 nodejs yarn \
&& mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Terminal
% touch Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>6'
Terminal
% touch Gemfile.lock
The contents can be empty
Terminal
% touch entrypoint.sh
entrypoint.sh
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Terminal
% touch docker-compose.yml
docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- '3306:3306'
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql-data:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
stdin_open: true
tty: true
volumes:
mysql-data:
driver: local
Terminal
docker-compose run --no-deps web rails new . --force --database=mysql
config/database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: 5
username: root
password: password
host: db
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
It is necessary to execute this command when writing in Gemfile and Dockerfile
Terminal
% docker-compose build
Terminal
% docker-compose run web rails db:create
Terminal
% docker-compose up
Complete if the welcome page is always displayed on localhost: 3000 You may be required to install webpacker etc. on the way
Recommended Posts