# Recommended recipe for deploying a Julia app in Docker with efficient precompilation?

**URL:** <https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591>\
**Category:** General Usage\
**Tags:** docker\
**Created:** [March 6, 2023, 5:40am UTC](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591 "2023-03-06T05:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [March 6, 2023, 5:40am UTC](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591/1 "2023-03-06T05:40:16Z")

</div>

Now that Julia 1.9/10 precompilation is doing more the first time a package is used, I’m seeing rather slow (7-8 minutes) restarts on a production docker image of my Julia app. When I run it locally with packages that have already been precompiled, the startup time is much faster.

Is there a recommended way to build Julia docker images that will

1. Do all precompilation during image creation rather than when a image is run, and
2. Cache as much dependency precompilation as possible, so that rebuilding a docker image only needs to update the precompilation for my app’s own code?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 6, 2023, 7:34am UTC](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591/2 "2023-03-06T07:34:41Z")

</div>

> [@robsmith11](#):
>
> Is there a recommended way to build Julia docker images that will
> 
> 1. Do all precompilation during image creation rather than when a image is run,

Simply running `Pkg.instantiate` in your project should do the trick. I.e something like

```julia
COPY .... # copy the sources from your project into the Docker image
RUN julia --project="/path/to/my/project" \
          -e "import Pkg; Pkg.instantiate()"

```

  

> [@robsmith11](#):
>
> 1. Cache as much dependency precompilation as possible, so that rebuilding a docker image only needs to update the precompilation for my app’s own code?

I’m less sure about this one. Perhaps you could copy the `Project.toml` file from your project (and perhaps also the `Manifest.toml` if you version it) to a temporary directory in the Docker image in an earlier stage, and `Pkg.instantiate` it at that time. This way, Docker should cache the results from this stage (including all precompiled packages) and when you later instantiate your real project, Julia/Pkg should find most of the dependencies already precompiled (provided their versions didn’t change inbetween).

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [March 10, 2023, 1:47pm UTC](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591/3 "2023-03-10T13:47:21Z")

</div>

Thanks! That works well and for the caching, I just copied over the Project.toml and Manifest.toml before calling `Pkg.instantiate()` and then copied all other project source files after. Rebuilding after source changes that don’t touch the \*toml files now takes 1-2 seconds.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [October 10, 2024, 8:38pm UTC](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591/4 "2024-10-10T20:38:22Z")

</div>

I have a solution that allows you to benefit from precompilation cache even if you are building on a different machine!

> [@Creating a Docker Base Image to Accelerate Deployments](https://discourse.julialang.org/t/creating-a-docker-base-image-to-accelerate-deployments/121165):
>
> In my experience, deploying Julia to the cloud can be frustrating because it can be slow to build the container used to your app. [I](https://discourse.julialang.org/t/recommended-recipe-for-deploying-a-julia-app-in-docker-with-efficient-precompilation/95591) don’t [seem](https://discourse.julialang.org/t/julia-docker-aws-devops-workflow/109459/2) to be [the only one](https://discourse.julialang.org/t/rfc-cloud-container-infrastructure-management/112685/9). A major gripe is that AWS CodeBuild can’t do layer caching in a VPC, so strategies for rearranging your DockerFile to benefit from cache doesn’t help. I finally got a working solution for me. I build a base image with a SysImage of my dependencies and then copy my current src directory on deployment. Posting it here in case it is help…
