Rails 5(with Webpacker)/MySQL/Redis/nginxのDockerfile最小構成


```yaml:docker-compose.yml
version: '2.2'
services:
  db:
    environment:
      - MYSQL_ROOT_PASSWORD=docker
      - MYSQL_PASSWORD=docker
      - MYSQL_USER=docker
      - MYSQL_DATABASE=project_development
    build: ./docker/mysql

  redis:
    image: redis:4.0.6

  app:
    build: .
    command: '/app/scripts/start-server.sh'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
      - "3035:3035"
    links:
      - db
      - redis
    environment:
      - RAILS_DATABASE_USERNAME=root
      - RAILS_DATABASE_PASSWORD=docker
      - RAILS_DATABASE_NAME=project_development
      - RAILS_DATABASE_HOST=db
      - REDIS_HOST=redis
      - REDIS_PORT=6379

  nginx:
    build: ./docker/nginx
    command: nginx -g "daemon off;"
    links:
      - app
    ports:
      - "80:80"
```

```dockerfile:Dockerfile
FROM ubuntu:16.10
LABEL maintainer okmttdhr

ARG RUBY_VERSION=2.4.2
ARG NODE_VERSION v8.9.1
ARG NVM_VERSION v0.33.6
ARG YARN_VERSION 1.3.2
ENV TZ Asia/Tokyo

# install tools
RUN apt-get update && \
    apt-get clean && \
    apt-get install -y --no-install-recommends build-essential \
    libssl-dev libreadline-dev make mysql-client libmysqld-dev \
    tzdata vim-tiny libgdbm3 libgdbm-dev curl zlib1g-dev openssl \
    libqt5webkit5-dev git ca-certificates wget && \
    rm -rf /var/lib/apt/lists/*

# set timezone JST
RUN echo "${TZ}" > /etc/timezone \
  && rm /etc/localtime \
  && ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
  && dpkg-reconfigure -f noninteractive tzdata

# install ruby-build
RUN git clone https://github.com/rbenv/ruby-build.git /tmp/ruby-build && \
    cd /tmp/ruby-build && \
    ./install.sh && \
    cd / && \
    rm -rf /tmp/ruby-build

# install Node.js
ENV NVM_DIR $HOME/.nvm
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/$NVM_VERSION/install.sh | bash && \
  . $NVM_DIR/nvm.sh && \
  nvm install $NODE_VERSION && \
  nvm alias default $NODE_VERSION && \
  nvm use default
ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

# install yarn
RUN wget https://yarnpkg.com/downloads/${YARN_VERSION}/yarn_${YARN_VERSION}_all.deb && dpkg -i yarn_${YARN_VERSION}_all.deb

WORKDIR /app

# Install ruby & base gems
RUN CONFIGURE_OPTS="--disable-install-doc" ruby-build -v $RUBY_VERSION /usr/local && \
    gem install bundler rubygems-bundler --no-rdoc --no-ri && \
    gem regenerate_binstubs && \
    gem update --system && \
    gem update bundler && \
    rm -rf /tmp/ruby-build*

# use bundle container & set RAILS_ENV
ENV BUNDLE_GEMFILE=/app/Gemfile \
    BUNDLE_JOBS=2 \
    BUNDLE_PATH=/bundle \
    RAILS_ENV=development

# bundle
ADD Gemfile* /app/
RUN bundle install --retry 5
```

```sh:scripts/start-server.sh
#!/bin/bash
cd /app
bundle install

# remove pid file
if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

if [ "${RAILS_ENV}" != "development" ]; then
  bundle exec rails db:migrate

  echo "Starting server... RAILS_ENV: $RAILS_ENV"
  bundle exec rails s -p 3000 -b '0.0.0.0' -h
  tail -f /app/log/${RAILS_ENV}.log
else
  bundle exec foreman start -f ./Procfile.dev
fi
```

```:Procfile.dev
app: bundle exec rails s -p 3000 -b '0.0.0.0'
webpacker: bin/webpack-dev-server
```

```yaml:.circleci/config.yml
defaults: &defaults
    working_directory: ~/app
    docker:
      - image: circleci/ruby:2.4.2-node-browsers
      - image: circleci/node:8.9.1
        environment:
          TZ: /usr/share/zoneinfo/Asia/Tokyo
          RAILS_ENV: test
          RAILS_DATABASE_USERNAME: root
          RAILS_DATABASE_PASSWORD: docker
          RAILS_DATABASE_NAME: project_test
          RAILS_DATABASE_HOST: 127.0.0.1
          NODE_PATH: ./
      - image: circleci/mysql:5.7.20
        environment:
          TZ: /usr/share/zoneinfo/Asia/Tokyo
          MYSQL_ROOT_PASSWORD: docker
          MYSQL_PASSWORD: docker
          MYSQL_USER: rails
          MYSQL_DATABASE: project_test
version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - setup_remote_docker:
          reusable: true
      - restore_cache:
          name: restore bundle cache
          key: gemfile-{{ checksum "Gemfile.lock" }}
      - run:
          name: bundle install
          command: bundle install --jobs=4 --path=vendor/bundle
      - save_cache:
          name: save bundle cache
          key: gemfile-{{ checksum "Gemfile.lock" }}
          paths:
              - vendor/bundle
      - restore_cache:
          name: restore yarn cache
          key: yarnlock-{{ checksum "yarn.lock" }}
      - run:
          name: yarn install
          command: yarn
      - save_cache:
          name: save yarn cache
          key: yarnlock-{{ checksum "yarn.lock" }}
          paths:
              - node_modules
              - .cache/yarn/
      - run:
          name: pwd and env
          command: pwd && env | sort
      - run:
          name: ls
          command: ls
      - run:
          name: run test
          command: |
            circleci tests glob 'spec/**/*_spec.*' \
              | circleci tests split --split-by=timings --timings-type=filename \
              | tee -a /dev/stderr \
              | xargs bundle exec rspec \
              --profile 100 \
              --format progress && \
              yarn test
      - persist_to_workspace:
          root: ~/app
          paths:
              - ./*

  deploy:
    working_directory: ~/app
    docker:
      - image: docker:17.09-git
    steps:
      - attach_workspace:
          at: ~/app
      - run:
          name: deployment
          command: echo 'デプロイ'
workflows:
  version: 2
  build_and_test:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - master
                - release
```