2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dokcerによる Rails:6.0, MySQL:8.0の環境構築

Posted at
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 "$@"
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?