Caching dependency compilation in Docker build

I’m deploying a Phoenix app with Elixir releases and Docker. The Phoenix docs have an example dockerfile you can use – mine is based on that one.

I’ve set up the file so that dependency downloads and compilation should be cached by Docker, but this isn’t happening. This is slowing down deploys a bit. Can anyone spot why Docker isn’t caching dependencies? Even when mix.exs is unchanged, it redownloads and rebuilds them.

FROM elixir:1.9.0-alpine as build

# install build dependencies
RUN apk add --update git build-base

# prepare build dir
RUN mkdir /app
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force && \
  mix local.rebar --force

# set build ENV
ENV MIX_ENV=prod

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get
RUN mix deps.compile
COPY config config

# build assets
# COPY assets assets
# RUN cd assets && npm install && npm run deploy
# RUN mix phx.digest

# build project
COPY priv priv
COPY lib lib
RUN mix compile

# build release
COPY rel rel
RUN mix release

Do you touch the mix.lock or mix.exs between builds?

From a theoretical point of view, your file should work.

Interesting. Nope, the dependencies get redownloaded and rebuilt even when I haven’t touched those files – that’s why I’m confused and why I opened this thread!

The Phoenix docs actually have a slightly different order:

COPY mix.exs mix.lock ./
COPY config config
RUN mix deps.get
RUN mix deps.compile

I moved this around since I didn’t want to redownload deps after changing configuration, but now I’m starting to wonder if there’s a reason to their ordering?

Oh, yes. Config needs to be available, as it might affect how dependencies are built.

Can you give an example where a config’s value would determine how dependencies are built?

I know you can choose to install packages based on the environment with mix but isn’t that controlled by the mix command itself with --only prod along with mix.exs?

Compile time configuration is the magic word.

1 Like

Okay, I’ll try going back to the original config from the docs!

I’m not sure I understand how this could make Docker not cache things, though. All that Docker knows is that nothing has changed in mix.exs and mix.lock, yet it still re-runs the dependency steps instead of using the cached versions.

Yeah, I do not understand that as well… Weird.

As an example, Logger lets you remove log calls from modules at compile time, even in dependencies, through the config option :compile_time_purge_matching.

https://hexdocs.pm/logger/Logger.html#module-application-configuration

Thanks, but would this change which dependencies get mix installed?

Oh, sorry, I was just respond to your comment “determine how dependencies are built” and giving an example of the compile time configuration @NobbZ mentioned. Not sure how config would affect which dependencies get installed though, since that’s the job of the mix.exs and mix.lock.

the Docker COPY command should perform a checksum of the given source files, and use that for caching. It should not even check the creation/modification time of the files.

The RUN command should only use the command string to determine whether to use the cache or re-run the command.

Therefore, the dependencies will be re-fetched if any of the following conditions apply:

  • The Dockerfile line RUN mix deps.get or any Dockerfile command appearing before it was modified

  • The content of mix.exs or mix.lock changed

As I understand, you say that none of those happened, and the dependencies are still re-fetched. In order to understand what is going on, it would be interesting to check:

  • What is the first Dockerfile command to invalidate the cache? Is it RUN mix deps.get or something even earlier?

  • If you separate the COPY mix.exs mix.lock ./ into two different COPY commands, which one uses the cache and which does not?

I hope we can guess what’s wrong with this information :slight_smile:

Another possibility could be that the environment where you are building the docker image does not support caching (e.g. on CI it is usually necessary to configure caching of artifacts, otherwise every build starts from a fresh context and won’t find any cached layer to reuse)

@tao did you manage to find out what the problem was? I am curious :slight_smile:

The first command to not use the cache is the first COPY:

remote: Sending build context to Docker daemon  162.3kB        
remote: 
remote: 
remote: Step 1/26 : FROM elixir:1.9.0-alpine as build        
remote:  ---> 7a6d28e4b511        
remote: Step 2/26 : RUN apk add --update git build-base        
remote:  ---> Using cache        
remote:  ---> 11da61788331        
remote: Step 3/26 : RUN mkdir /app        
remote:  ---> Using cache        
remote:  ---> 2a0fd2bc9f5c        
remote: Step 4/26 : WORKDIR /app        
remote:  ---> Using cache        
remote:  ---> 876adb368066        
remote: Step 5/26 : RUN mix local.hex --force &&   mix local.rebar --force        
remote:  ---> Using cache        
remote:  ---> eb75832955a0        
remote: Step 6/26 : ENV MIX_ENV=prod        
remote:  ---> Using cache        
remote:  ---> 3bc485b4dc82        
remote: Step 7/26 : COPY mix.exs mix.lock ./        
remote:  ---> 223512ca1219        
remote: Step 8/26 : COPY config config        
remote:  ---> 09c813490fe9        
<snip>

I’m just separating it into two commands like you suggested now. I’ll update here when I try this out! FWIW this is running on Dokku, so caching should be supported.

1 Like

It’s the first COPY command that isn’t cached:

remote: Step 1/27 : FROM elixir:1.9.0-alpine as build        
remote:  ---> 7a6d28e4b511        
remote: Step 2/27 : RUN apk add --update git build-base        
remote:  ---> Using cache        
remote:  ---> 11da61788331        
remote: Step 3/27 : RUN mkdir /app        
remote:  ---> Using cache        
remote:  ---> 2a0fd2bc9f5c        
remote: Step 4/27 : WORKDIR /app        
remote:  ---> Using cache        
remote:  ---> 876adb368066        
remote: Step 5/27 : RUN mix local.hex --force &&   mix local.rebar --force        
remote:  ---> Using cache        
remote:  ---> eb75832955a0        
remote: Step 6/27 : ENV MIX_ENV=prod        
remote:  ---> Using cache        
remote:  ---> 3bc485b4dc82        
remote: Step 7/27 : COPY mix.exs ./        
remote:  ---> 2561f54cdafa
<snip>        

This seems to be an issue with Dokku, and not Elixir, so I’m continuing debugging there: https://github.com/dokku/dokku/issues/3631

Does it cache using docker instead? Just basic bare docker?

I think so, but not sure. Looks like they’ve had some issues with multi-stage builds (like this one) in the past.

The provided dockerfile is not multistage. Have you tried using docker or are you just guessing?

Ah yeah, I just copied the build stage – I’m using the same one as in the Phoenix docs, which is multistage.

Just tried with docker on my local machine. Caching works as expected there, so this must be a Dokku problem!