DockerFileメモ

メモ内容

開発時は、Nuxt + Railsで、本番はnuxt build したものを、RailsのPublicフォルダに渡すみたいなことをする時のDockerFileメモ。 実際使うときは、Nuxtビルドしたものは、S3とかに置くと思うので、使い道はないかもしれないけど...

開発時

Nuxt

FROM node:10.13.0-alpine
ENV LANG C.UTF-8

# Create app directory
RUN mkdir /frontend
WORKDIR /frontend

ADD . /frontend

RUN npm i -g yarn
ADD yarn.lock /frontend/yarn.lock
RUN yarn install

CMD yarn run dev

Rails

FROM ruby:2.5.3-alpine3.8

RUN cd /tmp \
  && apk --no-cache add \
    curl \
    curl-dev \
    libstdc++ \
    libxml2-dev \
    libxslt-dev \
    linux-headers \
    mysql-client \
    mysql-dev \
    postgresql-client \
    postgresql-dev \
    pcre \
    ruby-dev \
    ruby-json \
    tzdata \
    yaml \
    yaml-dev \
    bash \
    build-base \
    zlib-dev

# Rails App
RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --jobs=4

ADD . /app

RUN mkdir -p tmp/sockets
RUN mkdir -p tmp/pids

# Expose volumes to frontend
VOLUME /app/public
VOLUME /app/tmp
VOLUME /app/log

# Start Server
CMD bundle exec puma -e local

DockerCompose

version: "2"
services:
  app:
    build: .
    image: rails
    volumes:
      - .:/app
    ports:
      - "3000:3000"

  nuxt:
    image: nuxt
    build: frontend
    ports:
      - "4000:4000"
    volumes:
      - .:/frontend
      - node-modules-data:/frontend/node_modules

volumes:
  node-modules-data:

本番

# build
FROM node:10.13.0-alpine AS build-html

# node-sassのためにpython2が必要
RUN apk --no-cache add python

RUN mkdir /fontend
ADD frontend /frontend

WORKDIR /frontend
RUN npm i -g yarn
RUN yarn install
ARG ENV
RUN yarn run build:production

# RUN
FROM ruby:2.5.3-alpine3.8

RUN cd /tmp \
  && apk --no-cache add \
  curl \
  curl-dev \
  libstdc++ \
  libxml2-dev \
  libxslt-dev \
  linux-headers \
  mysql-client \
  mysql-dev \
  postgresql-client \
  postgresql-dev \
  pcre \
  ruby-dev \
  ruby-json \
  tzdata \
  yaml \
  yaml-dev \
  bash \
  build-base \
  zlib-dev

RUN mkdir /app
WORKDIR /app

ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --jobs=4

ADD . /app

RUN mkdir -p tmp/sockets
RUN mkdir -p tmp/pids

# copy build html file
COPY --from=build-html /frontend/public /app/public

# Expose volumes to frontend
VOLUME /app/public
VOLUME /app/tmp
VOLUME /app/log

# Start Server
ARG ENV
CMD bundle exec puma