20 July 2016 · About 1 minute read

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

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 gem1 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 

Learn how to keep your Rails apps fast, secure and running in top condition.

The Ruby and Rails worlds move fast, so it can feel like no time at all before your app starts to feel left behind.

Slow pages, memory, new gems, a constant stream of security patches, changing third-party APIs, and growing demands on your server's resources all work tirelessly against your app.

While your app launched with a strong foundation— the latest gems, employing best practices, and developing a thorough test suite—it needs constant attention and maintenance to keep running in top condition.

In Keep Your Ruby on Rails App Healthy, you'll learn about the tools and techniques for keeping your Ruby on Rails app running smoothly, and how to apply them to your apps.

The course delivers a range of practical and actionable lessons to enhance and protect your app right away, from simple, quick–win optimisations and setting up thorough monitoring to reviewing your site's performance, and checking your code against security vulnerabilities current best practices.

“I really enjoyed the book. A lot of quick wins for me to implement and makes me realise I can do more with guard.” –– Ben (@r0_0tKat)

Keep Your Ruby on Rails App Healthy is available as both an email course, and as a premium download edition.

The email course is split up into 7 lessons, delivered to your inbox over the course of a month.

The premium edition is available for instant download in PDF, ePub and Mobi (Kindle) format.

Start Email Course Buy Premium $9 (plus EU VAT)

rails docker bundler
Chris Blunt
Chris Blunt @cblunt
Chris is the founder of Plymouth Software. As well as code and business, he enjoys being a Dad, swimming, and the fine art of drinking tea.