- M1 mac
- Ruby 2.7.4
- Rails 6(latest)
- MySQL 5.7
# syntax=docker/dockerfile:1
FROM ruby:2.7.4
RUN wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y nodejs yarn
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# 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
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
version: '3.9'
services:
db:
platform: linux/x86_64
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: 'password'
volumes:
- mysql:/var/lib/mysql
ports:
- "3306:3306"
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
redis:
image: redis:6
ports:
- "6379:6379"
volumes:
- redis:/var/lib/redis/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
- bundle:/usr/local/bundle
ports:
- "3000:3000"
depends_on:
- db
environment:
REDIS_URL: redis://redis:6379
tty: true
stdin_open: true
sidekiq:
build: .
command: bundle exec sidekiq
volumes:
- .:/myapp
- bundle:/usr/local/bundle
depends_on:
- db
- redis
environment:
REDIS_URL: redis://redis:6379
volumes:
bundle:
mysql:
redis:
#!/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 "$@"
source 'https://rubygems.org'
gem 'rails', '~>6'
docker-compose run --no-deps web rails new . --force --database=mysql
vim config/database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: 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'] %>
docker-compose up --build
docker-compose exec web rails db:create
docker-compose logs web -f
docker-compose exec web bundle exec rails g scaffold page name:string title:string
docker-compose exec web bundle install
docker-compose exec web /bin/bash
docker attach rails-app-web-1
- remove unused image/volume
docker image prune
docker volume prune