The environment created by Sample in docker was Ruby2.5 + Rails5 + PostgreSQL, which was old and PostgreSQL, so It is rewritten for each new version Ruby2.7 & Rails6.0 & MySQL8.0.
myapp
and replace it if you want to change it.FROM ruby:2.7
# or latest
#nodejs and mysql-client (default-mysql-client)Installation of
RUN apt-get update -qq && apt-get install -y curl apt-transport-https wget nodejs default-mysql-client
#Install Yarn
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 yarn
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
mariadb-client
is okay for default-mysql-client
(see below)yarn
source 'https://rubygems.org'
gem 'rails', '~>6.0.0'
$ touch Gemfile.lock
COPY Gemfile.lock /myapp/Gemfile.lock
described in Dockerfile
will fail.Step 7/14 : COPY Gemfile.lock /myapp/Gemfile.lock
ERROR: Service 'web' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder424702045/Gemfile.lock: no such file or directory
#!/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 "$@"
version: '3'
services:
db:
image: mysql:8.0 # or lates:
environment:
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
volumes:
- db-data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- ${DB_PORT}:3306
web:
build: .
env_file: .env
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
- bundle-data:/usr/local/bundle
ports:
- "3000:3000"
depends_on:
- db
volumes:
db-data:
bundle-data:
MYSQL_ROOT_PASSWORD
must be specified at a minimum$ {variable name}
to refer to the value set in the .env
file described later.--default-authentication-plugin = mysql_native_password
by changing the default authentication method., but ʻenv_file: .env
is also possible.DB_ROOT_PASSWORD=mysql_root_password
DB_USER=myapp
DB_PASSWORD=myapp_password
DB_PORT=3306
docker-compose.yml
This completes the file preparation. Here is what I have described so far. https://github.com/madai0517/rails6-mysql8_docker
rails new
$ docker-compose run web rails new . --force --no-deps --database=mysql --webpacker --skip-test
$ sudo chown -R $USER:$USER . #Required for Linux. Not required if you're on a Mac
--database = mysql
.--skip-test
because I think I often use Rspec instead of Minitest--webpacker
, you will need to run $ docker-compose run web rails webpacker: install
later.$ brew services stop mysql
(mysql part is appropriately named Formulae)default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: db
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
.env
db
specified in host
is the same as the db
described in docker-compose.yml
.$ docker-compose build
$ docker-compose up -d
$ docker-compose exec db mysql -uroot -p -e"GRANT ALL PRIVILEGES ON *.* TO 'myapp'@'%';FLUSH PRIVILEGES;"
$ docker-compose run web rails db:create
DB_USER
(myapp
in this case) after launching the containerMYSQL_USER
but didn't know how to write it ...This completes the environment construction. You should be able to see the usual screen by accessing http: // localhost: 3000 /.
Recommended Posts