Cookbooks
It is official. Rails 7.1 will have default dockerfiles. While there is much polishing to be done, the direction is clear: going forward, Rails applications will include Dockerfiles.
For a large number of people that will move the task of learning Dockerfiles from something that needs to be done someday to something that is important to have at least a working knowledge of relatively soon.
There is a lot of good docs out there. A good place to start is with Rails on Docker which will walk you through the current Rails template.
Fly Rails cookbooks are for those that wish to explore further. In each, you will start with a minimal Dockerfile that focuses on one topic. And you will be introduced to a small number of new alternatives, exploring different tradeoffs or use cases.
While this tutorial is anchored by focusing on deploying Ruby on Rails on fly.io, the concepts apply to other frameworks and other cloud providers.
While you are welcome to explore these cookbooks in any order, if the concept of multi-stage docker builds is new to you starting with the first cookbook will make understanding the rest easier.
If you are comfortable skipping the first cookbook, all cookbooks start with an
empty directory and a seed Dockerfile
. Run fly launch
to create an
application, and then proceed from there, running fly deploy
after you make
changes. The recipes the follow contain fragments that can be added
to multiple cookbooks.
For best results, you are encouraged to try out each step. To do so, you will need to Log in to Fly. Nothing you do in this tutorial will exceed the Free Allowances provided.
These cookbooks can be more than mere educational materials. Using throwaway applications is
often better than experimenting in production when you want to make configuration changes.
Starting a minimal application, using flyctl ssh console
to shell into that machine and
explore and make changes, rerunning fly deploy
to reset the machine when those experiments
fail and making changes to your Dockerfile
and fly.toml
when things work is an effective
way to validate an approach before making changes to your application.
Let’s get started.
Cookbooks
- Minimal - full ruby, slim ruby, multi-stage build
- Databases - sqlite3, postgresql, mysql
- Node - support for esbuild, rollup, webpack
- API - api only rails servers with web front end
Recipes
- Sources - adding sources from outside of your
Dockerfile
. - Optimizing builds - ordering, splitting, staging, and caching.
- Optimizing deployments - jemmalloc, yjit, swap.
A Rails generator incorporating these techniques can be found at dockerfile-rails, and is being proposed as the generator to be included by default in Rails 7.1.