tao

tao

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

First Post! Switch mode

NobbZ

NobbZ

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

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

Most Liked

NobbZ

NobbZ

Compile time configuration is the magic word.

tao

tao OP

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.

Last Post!

junaid1460

junaid1460

Whoever is coming now.
I have an umbrella app. Just mix.exs and mix.lock file copying won’t allow me to get all deps. So what I do is copy mix.exs from all the sub apps and make same structure like umbrella app but for configs of these sub apps I copy umrellas config. Then do

run mix deps.get --all
mix deps.compile

after this

copy apps /build/apps

digest build etc

from this stage onwards it’s always new. Again to be specific my docker file multistaged, so in second stage I install bare minimums are copy stuff from first stage

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement