The Downside to Our Existing Approach
Let's review the approach we have been using for installing gems.
Gem management
We now have a working development environment, completely based on Docker. However, there is one area that is worth a bit more thought: gem management.
Up until now, to install or update gems, we have simply been rebuilding the image for our Rails app. This works because bundle install is one of the steps in our Dockerfile. However, as we will see in a moment, there is a slight downside to this approach compared to what we are used to when managing gems in a non-Dockerized environment.
We will explore an alternative approach to managing our gems, which attempts to avoid the drawback by making different trade-offs (it’s more complicated for a start). Whether you stick with the simple approach we have been using so far or this new approach is completely up to you. We will look at the technique, learn about the trade-offs, and you can choose based on your needs and preferences.
The downside to our simple approach
Our current approach for managing gems is to rebuild the image any time we need to update our gems. However, you may have noticed that any time we change our Gemfile, even if it is just adding a single gem, all our gems have to be reinstalled from scratch. As a result, updating our gems takes longer than we are typically used to.
Why does this happen?
Bundler and Docker images are both trying to achieve the same goal of ensuring a consistent environment, but they achieve it in different ways. Docker’s image-build process breaks some of Bundler’s key assumptions, which means it does not quite work how we are used to.
Bundler
Bundler was primarily designed for use on a long-running system where installed gems hang around. The main use case is updating the set of currently installed gems. However, rebuilding an image is akin to building a new machine from scratch. In this scenario, Bundler is not being used on a long-running system. This, in essence, is the source of the issue.
Reviewing the Dockerfile
Let’s have another look at our Dockerfile and walk through what happens when we rebuild our image. Here are the last few lines:
Get hands-on with 1400+ tech skills courses.