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?

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What did you do to achieve this?
Ok, found it: set environment variable CARGO_HOME
Do you by chance know how to do that from the Dockerfile?
Vou can define volumes inside the Dockerfile (stackoverflow.com/a/46992367/15220905), but this is not what would help, since by doing that, they are just temporary. Use the -voption in your docker command instead.
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.

6 Comments

error: no such subcommand: `+nightly`
I'm sure this works. Please write the full command that you tried to execute.
cargo +nightly run -Z no-index-update. I have cargo 1.60 if it helps
Try rustup show and see if you actually have a nightly toolchain installed.. Although I've no idea what's going on here..
Probably a sign that Rust was not installed using rustup (and that you should start using rustup)
|
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.

2 Comments

Can I also set the CARGO_HOME environment variable from my Dockerfile instead? I'm using docker-compose instead of docker run.
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. This environment key in docker-compose is the same as the -e option when calling docker.
0

Another option is to run cargo update in your Dockerfile. However, this has some implications: you would have to mount or copy your project into the container that becomes the image - a simple rm project afterwards will NOT remove it from the image. It will stay part of a previous layer. But if the code can be in the image, because the image might be private or you don't consider it to be a problem, then

RUN mkdir /build COPY YOUR-APP /build WORKDIR /build RUN cargo update 

can store the index at build-time.

Also consider that this will only work for some time. At some point, the index will change again and you have to rebuild your image.

All in all, this is a workaround. But skipping the update step completely is either. So it should be fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.