11

I have a simple Program written in Rust. When I type cargo run in terminal it always shows:

Updating crates.io index...

And this takes around 40 seconds. But I just wan to execute my Program and I think cargo does not need to update the index every time I run the Program, since this makes testing very slow...

Is there an option to skip that?

thefeiter
  • 329
  • 2
  • 6

3 Answers3

8

I figured it out:

Since I am running cargo in a Docker container, I need to store the cargo cache persistently because it resets every time the container restarts.

thefeiter
  • 329
  • 2
  • 6
4

There is The Cargo Book that contains all the information you'd ever want to know about cargo. See this for disabling index update.

I've tried to use this feature myself, and here's the command that worked:

cargo +nightly run -Z no-index-update

The +nightly thing is new to me as well, but I find it here.

Mossa
  • 1,656
  • 12
  • 16
3

This answer has been brought up by users thefeiter and Captain Fim but I think a more complete answer could be cool rust/linux newcomers

When we use docker run, the index is updated every time the container is run because the cache is not shared between runs. So to skip the index update, as Captain Fim mentioned, you need to set the CARGO_HOME environment variable on the container. This environment variable should contain the path to a persistent folder. One simple solution is using the docker volumes to share cache between host and container.

In my case, I created at cargo_home folder in my project (could be somewhere else) on my host. I have passed the whole project folder to the container and set the docker env variable of CARGO_HOME to the container path to the cargo_home folder.

The command to build my app looks like this

docker run --rm --user "$(id -u)":"$(id -g)" -e CARGO_HOME=/usr/src/myapp/cargo_home -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust-compiler cargo build

The first time you will run this command, it will take some time, but you should see the cargo_home folder getting filled with files. The next time you run the command, it should use the cargo_home folder as cache. This should be almost instant if your app source code did not change.

Lunfel
  • 2,499
  • 21
  • 29
  • Can I also set the `CARGO_HOME` environment variable from my `Dockerfile` instead? I'm using `docker-compose` instead of `docker run`. – pfincent Nov 24 '22 at 22:29
  • If you are reffering to the ARG command in the Dockerfile, then I don't think this is the good approach. IIRC, ARG in Dockerfile is more of a env variable when the container is being built (before running the built container). If you are using docker-compose then you can use the `environment` key on your service in the docker-composer.yml. See [doc here](https://docs.docker.com/compose/environment-variables/#set-environment-variables-in-containers). This `environment` key in docker-compose is the same as the `-e ` option when calling docker. – Lunfel Nov 25 '22 at 23:45