Skip to content

Instantly share code, notes, and snippets.

@vesan
Forked from tobi/Dockerfile
Created May 11, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vesan/470f67310e28022f5894ca3bf575950f to your computer and use it in GitHub Desktop.
Save vesan/470f67310e28022f5894ca3bf575950f to your computer and use it in GitHub Desktop.
# Rails production setup via SQLite3 made durable by https://litestream.io/
# Copy this to Dockerfile on a fresh rails app. Deploy to fly.io or any other container engine.
#
# try locally: docker build . -t rails && docker run -p3000:3000 -it rails
#
# in production you might want to map /data to somewhere on the host,
# but you don't have to!
#
FROM ruby:3.0.2
# use https://github.com/benbjohnson/litestream/releases/download/v0.3.7/litestream-v0.3.7-linux-amd64-static.tar.gz on intel
ADD https://github.com/benbjohnson/litestream/releases/download/v0.3.7/litestream-v0.3.7-linux-arm64-static.tar.gz /tmp/litestream.tar.gz
RUN tar -C /usr/local/bin -xzf /tmp/litestream.tar.gz
ENV RAILS_ENV 'production'
ENV DB_PATH '/data/production.sqlite3'
# find a REPLICA_URL host/keys setup for persisting your sqlite3 database ( https://litestream.io/guides/ )
# supports sftp, s3, azure, google cloud storage, backblaze, etc. you probably have an account
# already
ENV REPLICA_URL 's3://<your-url>/db.sqlite3'
ENV LITESTREAM_ACCESS_KEY_ID '<your-access-key-id>'
ENV LITESTREAM_SECRET_ACCESS_KEY '<your-secret-access-key>'
# get dependencies
WORKDIR /app
ADD Gemfile /app/
ADD Gemfile.lock /app/
RUN bundle install
# add code (and bundle)
ADD . /app
RUN bundle exec rake assets:precompile
# rails expects production.sqlite3 to be in db/production.sqlite3
RUN ln -nfs $DB_PATH /app/db/production.sqlite3
EXPOSE 3000
CMD \
# if the db file doesn't exist we get it from the REPLICA_URL
[ ! -f $DB_PATH ] && litestream restore -v -if-replica-exists -o $DB_PATH "${REPLICA_URL}" \
# then we run the migrations
; bundle exec rake db:migrate \
# then we launch replicate and execute rails server
; litestream replicate -exec "bundle exec rails server -p 3000" $DB_PATH $REPLICA_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment