Skip to content

Instantly share code, notes, and snippets.

@patio11
Last active March 23, 2022 20:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patio11/6fbb95b0cb5ccd186de8526a7bc8260d to your computer and use it in GitHub Desktop.
Save patio11/6fbb95b0cb5ccd186de8526a7bc8260d to your computer and use it in GitHub Desktop.
Dockerfile for using Rails 6 with Fly plus a Tailscale tunnel, with comments. Put fly_start.sh in your bin directory within the Rails app.
# Heavily based off of https://github.com/fly-apps/rails-nix/blob/main/Dockerfile
# I have replaced the image from Fly's above file with rubylang's default one
# so that I know it has compatibility across platforms.
ARG RUBY_VERSION=2.6.8
FROM rubylang/ruby:${RUBY_VERSION}-bionic as patio11-build
# Copied from https://github.com/fly-apps/rails-nix/blob/main/Dockerfile
ENV PATH $PATH:/usr/local/bin
# Since Rails 6 requires some JS, installing Node, NPM, and Yarn.
# Installs a specified version of Node to overwrite Ubuntu's.
RUN --mount=type=cache,id=apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y \
git build-essential libpq-dev wget vim curl gzip xz-utils \
libpq-dev nodejs npm && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
npm uninstall yarn -g && \
npm install yarn -g -y && \
npm install -g n && n 16.14.2
# Totally optional but I like Tailscale
# Building tailscale for Ubuntu bionic using commands from https://pkgs.tailscale.com/stable/#ubuntu-bionic
# Removes the apt lists/archives at end to minimize image size/drift.
# Note that I've removed sudo from the commands because we're running as root.
FROM ubuntu:bionic as tailscale
WORKDIR /tailscale
ENV TSFILE=tailscale_1.22.2_amd64.tgz
RUN --mount=type=cache,id=apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y \
wget ca-certificates
RUN wget https://pkgs.tailscale.com/stable/${TSFILE} && tar xzf ${TSFILE} --strip-components=1
# Bundle install
FROM patio11-build AS patio11-build-rails-app
RUN mkdir /app
WORKDIR /app
ENV BUNDLE_WITHOUT development:test
ENV BUNDLE_PATH vendor/bundle
RUN gem install -N bundler -v 1.17.2
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# There will be a Docker checkpoint made here (among other places), which will
# keep the image relatively slim, prior to the following command pulling down
# the entire world from npm.
WORKDIR /app
ENV BUNDLE_PATH vendor/bundle
RUN bundle exec rails assets:precompile
RUN rm -rf vendor/bundle/ruby/*/cache
FROM patio11-build-rails-app
ARG RAILS_ENV=production
ENV RAILS_ENV=${RAILS_ENV}
ENV RAILS_SERVE_STATIC_FILES true
ENV BUNDLE_PATH vendor/bundle
ENV BUNDLE_WITHOUT development:test
ENV RAILS_MASTER_KEY=${RAILS_MASTER_KEY}
RUN --mount=type=cache,id=apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y \
postgresql-client file vim curl gzip && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Since I can't seem to specify the working directory for release command
# I am putting it in the root of the docker image.
# COPY --from=patio11-build-rails-app /app /app
COPY --from=tailscale /tailscale/tailscaled /usr/local/bin/tailscaled
COPY --from=tailscale /tailscale/tailscale /usr/local/bin/tailscale
WORKDIR /app
RUN mkdir -p tmp/pids
EXPOSE 8080
CMD ["/bin/fly_start.sh"]
#!/bin/sh
# Starts tailscale. Some uncertainty on whether this will work with multiple boxes.
tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock &
tailscale up --authkey=${TAILSCALE_AUTHKEY} --hostname=fly-${FLY_APP_NAME}
# And now run the rails app.
bundle exec puma -C config/puma.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment