Lawrence Chou

Joined

10 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Docker Basics for Rails Discussion

Thanks for the great tutorial! I am a beginner in dockerizing rails app and get started with this tutorial. I realized some optimization may have been omitted to keep the tutorial concise. Following are some optimization tricks I found useful if starting from the Dockerfile :)

  1. Add (at least) node_modules and .git into a .dockerignore file, so the COPY . /app/ step could skip these files. (This could save you a log of time/docker image size when .git is huge)

  2. Avoid the error: 'A server is already running. Check /project/tmp/pids/server.pid. Exiting' with an entrypoint.sh script.
    Refer to Docker docs - Sample applications - Rails and PostgreSQL for the complete script, and don't forget to change #!/bin/bash to #!/bin/sh since we are using Alpine in this tutorial.

  3. Avoid needing to run yarn install again after change the source code and re-build image.
    Instead of:

COPY . /app/

ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install

do:

ENV BUNDLE_PATH /gems
COPY package.json yarn.lock /app/
RUN yarn install
COPY Gemfile Gemfile.lock /app/
RUN bundle install
 # Steps above could be cached most of the time when re-building image

 # Copy source code after dependencies installed
COPY . /app/