Plymouth Software Logo

Rails on Docker: Quickly Create or Update Your Gemfile.lock

Written by Chris • 20 July 2016

As I migrate all of my Rails apps to Docker, I've learned to avoid making changes to the Gemfile whenever possible.

Whilst there are workarounds that can help to speed things up, any change to your Gemfile results in a lengthy (re)download and install of all the gems in your app.

Sometimes I only needed to generate a new Gemfile.lock so that the image could be built, tested and published by our continuous deployment server. Frustratingly, this meant having to wait for the redundant local bundle install to complete before I could commit the new lock file.

Today, I discovered a way to save the hours wasted downloading gems: bundler's lock command.

This gem[^1] of a command resolves your app's dependencies and writes out the appropriate Gemfile.lock – without installing any of the gems themselves.

Using Bundle Lock with Docker

Use the following command in your app's path to quickly create your Gemfile.lock:

# cd /path/to/my-rails-app
$ docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app ruby:2.3.1 bundle lock

If you have an existing lock file, but want to update the gems (equivalent to bundle update), just add the --update flag:

# cd /path/to/my-rails-app
$ docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app ruby:2.3.1 bundle lock --update

Hopefully this will save you some time (and bandwidth) when building your Rails apps on Docker.

[^1]: Pun very much intended