How to cache bundle install with Docker
While dockering a Rails app, the first problem that comes out is the slow bundle install command while building the app’s image.
Let’s say you have this vanilla Dockerfile for a Rails app:
When you build this image with:
docker build -t app .
Docker creates a layer for each command executed. When you execute it a second time it will reuse the cache from previous execution if it did not change.
In this case there is a high probability that the ADD command will get a cold cache, because you change your app’s code all the time (hopefully).
So it will stop using the cache on that line and will execute again all the next commands, including the bundle install. In summary: slow.
Bundle install before app files
So, let’s use another approach: