Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Heroku rebuild my Docker container every time?

I'm deploying a Rust app with Rocket.rs in a Docker container to Heroku. Each time I make one small change, I have to push the entire container. This required re-downloading all the rust components (rustc, rust-std, cargo, etc.), redownloading all dependencies, and re-pushing layers. In particular, there is one 1.02 GB layer that is pushed every time, which takes about 30 minutes. Every time. How can I avoid:

  • Re-downloading rustc, rust-std, cargo, and rust-docs every time
  • Re-downloading the same, unchanged dependencies, every time
  • Re-pushing a 1.02 GB layer every time

Here is a Gist with all my relevant files: https://gist.github.com/vcapra1/0a857aac8f05277e65ea5d86e8e4e239

By the way I should mention my code is pretty minimal: (this is the only .rs file)

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;

use std::fs;

#[get("/")]
fn index() -> &'static str {
    "Hello from Rust!"
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}
like image 599
vcapra1 Avatar asked Jan 25 '19 16:01

vcapra1


1 Answers

Re-downloading rustc, rust-std, cargo, and rust-docs every time. Re-downloading the same, unchanged dependencies, every time

You should cache these steps.

Re-pushing a 1.02 GB layer every time

You doesn't need any of the Rust toolchains to run the compiled binary application, so you can simply use debian:8-slim or even alpine to run it.

This will reduce the image size to 84.4MB:

FROM rust:1.31 as build

RUN USER=root cargo new --bin my-app
WORKDIR /my-app

# Copy manifest and build it to cache your dependencies.
# If you will change these files, then this step will rebuild
COPY rust-toolchain Cargo.lock Cargo.toml ./
RUN cargo build --release && \
    rm src/*.rs && \
    rm ./target/release/deps/my_app*

# Copy your source files and build them.
COPY ./src ./src
COPY ./run ./
RUN cargo build --release

# Use this image to reduce the final size
FROM debian:8-slim

COPY --from=build /my-app/run ./
COPY --from=build /my-app/target/release/my-app ./target/release/my-app

CMD ["./run"]
like image 191
Artemiy Rodionov Avatar answered Nov 16 '22 01:11

Artemiy Rodionov