Is it possible to get fast Rust compiles in a Docker container?

69 points SnooPears7079

Full disclosure: I'm the author of this article, which got a ton of... mixed reviews here and elsewhere. I do greatly enjoy writing Rust. It's my go-to when developing CLI's. I just cannot get it to work for this usecase.

I personally love working on projects that run full-stack locally: I find the dev feedback loop is the best. I also like to be able to just docker-compose up my entire project, so I tend to develop with the services inside containers.

My issue with using rust for my backend right now is that the compiles in the container are so slow. They take around 1 minute for the basic actix-web project (when just compiling the project, none of the dependencies), which completely kills the feedback loop I'm looking for. I do the same exact thing with Golang and it works much, much faster: the compiles are almost unnoticeable.

Is what I'm asking for impossible? When I just compile outside the container it takes not even 2 seconds.

Here's the dockerfile in question (I'm on an M1 Mac, so I thought maybe using an arm container could help to skip translation)

and here's the Actix web project (although it's just the default starter project)

To be perfectly clear: I am not rebuilding the container from scratch over and over. I am compiling the project inside the container. As in:

Compiling fns v0.1.0 (/src)
 Finished dev [unoptimized + debuginfo] target(s) in 1m 28s 
 Running `target/debug/fns`

the vast majority of the advice on the reddit post about my article was how to speed up the container builds. The container builds are not that big of a deal to me! I thought I was clear about this, but since so many people thought that was what I was talking about, it was clearly a communication error on my part. Sorry about that.

EDIT: I guess I wasn't clear again. I am NOT talking about the first compile. When I make a change to the code, Cargo watch triggers and recompiles. I am talking about the 1 + nth compile!

EDIT 2: Here is docker compose:

version: '3.8'
services:
  fns:
    build:
      context: fns
    ports:
     - ${API_PORT}:${API_PORT}
    volumes:
    - ./fns:/src

The code changes get picked up because it is on a mount, so when I change the code on my local machine, it changes it inside the container as well.

EDIT 3: Found the problem! The problem is not Rust (it never is!) it's docker bind mounts being very, very slow on Mac. I just exec'd into a container and created a new project, not mounted to my machine. It compiled in 5s, which is as good as my host machine. Incredible. Not sure what to do from here but I will continue with this info and try to drill down the problem.

TIA!