docker-compose.yml
version: "3"
# .envファイルに環境変数は指定
services:
db:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=$DB_PASSWORD
- MYSQL_USER=$DB_USER
- MYSQL_PASSWORD=$DB_PASSWORD
- TZ=Asia/Tokyo
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- mysql-data:/var/lib/mysql
ports:
- 3306:3306
app:
build: .
command: /bin/bash -c "rm -f /$WORKDIR/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/$WORKDIR
- ./vendor/bundle:/$WORKDIR/vendor/bundle
environment:
- TZ=Asia/Tokyo
- DB_HOST=$DB_HOST
- DB_USER=$DB_USER
- DB_PASSWORD=$DB_PASSWORD
ports:
- $API_PORT:$CONTAINER_PORT
depends_on:
- db
tty: true
stdin_open: true
volumes:
mysql-data:
driver: local
Dokcerfile
Dokcerfile
FROM ruby:3.0
# yarnのインストール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
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 && apt-get install -y yarn
RUN apt-get update && apt-get install -y nodejs
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . ./
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"]
Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6.1'
Gemfile.lock
# empty
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 "$@"