Optimising cargo build times in Docker
<p>I am developing an API with Rust, and am managing the environments, including the external database with Docker. Every time I make a change to the API code, cargo rebuilds, and since Docker doesn't cache anything to do with the <code>ADD</code> statement to copy the Rust directory over to the container, it re-downloads all the packages, which is a fairly lengthy process since I'm using Nickel, which seems to have a boatload of dependencies. </p>

<p>Is there a way to bring those dependencies in prior to running <code>cargo build</code>? At least that way if the dependencies change it will only install what's required, similar to Cargo compiling locally.</p>

<p>Here's the Dockerfile I currently use:</p>

<pre><code>FROM ubuntu:xenial
RUN apt-get update &amp;&amp; apt-get install curl build-essential ca-certificates file xutils-dev nmap -y
RUN mkdir /rust
WORKDIR /rust
RUN curl https://sh.rustup.rs -s &gt;&gt; rustup.sh
RUN chmod 755 /rust/rustup.sh
RUN ./rustup.sh -y
ENV PATH=/root/.cargo/bin:$PATH SSL_VERSION=1.0.2h
RUN rustup default 1.11.0
RUN curl https://www.openssl.org/source/openssl-$SSL_VERSION.tar.gz -O &amp;&amp; \
    tar -xzf openssl-$SSL_VERSION.tar.gz &amp;&amp; \
    cd openssl-$SSL_VERSION &amp;&amp; ./config &amp;&amp; make depend &amp;&amp; make install &amp;&amp; \
    cd .. &amp;&amp; rm -rf openssl-$SSL_VERSION*
ENV OPENSSL_LIB_DIR=/usr/local/ssl/lib \
    OPENSSL_INCLUDE_DIR=/usr/local/ssl/include \
    OPENSSL_STATIC=1
RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN cargo build
EXPOSE 20000
CMD ./target/debug/api
</code></pre>

<p>And here's my Cargo.toml</p>

<pre><code>[profile.dev]
debug = true

[package]
name = "api"
version = "0.0.1"
authors = ["Vignesh Sankaran &lt;developer@ferndrop.com&gt;"]

[dependencies]
nickel = "= 0.8.1"
mongodb = "= 0.1.6"
bson = "= 0.3.0"
uuid = { version = "= 0.3.1", features = ["v4"] }
</code></pre>