Where is the .cargo folder in the rust docker image?

Viewed 928

So, I'm trying to get into embedded rust, for which I had to use the nightly version of rust, and modify my .cargo/config.toml to change the target device, and stuff. I decided to use docker, as I didn't want this interfering with my main installation. I don't know much about docker, but I'm assuming, it's quite similar to pipenv, where what I do with the docker image, doesn't affect anything outside it. (Unless I run the code) So, this is how my Dockerfile looks

FROM jdrouet/rust-nightly:buster-slim AS builder

WORKDIR /usr/source/myapp

COPY . .

RUN cargo build --release

CMD cargo run

When I run sudo docker build . -t name It gives me the error I used to get before modifying my .cargo/config.toml file, which is a good thing, I'm guessing, cuz now I can revert to my original configuration, and make the changes to this image's config files. But I'm not able to find the configuration files for this docker image. I don't know what WORKDIR does, but there is no folder called /source in my /usr directory

1 Answers

So, I'm trying to get into embedded rust, for which I had to use the nightly version of rust, and modify my .cargo/config.toml to change the target device, and stuff

You can put a file in the folder wherever/your/project/is/.cargo/config.toml, and it will only impact the project(s) in that directory.

source: Cargo Book

I don't know much about docker, but I'm assuming, it's quite similar to pipenv

Docker is actually quite different to Pipenv. Cargo is similar to Pipenv in that it manages your dependencies for you (Cargo.toml vs Pipfile), distinguishes between regular dependencies vs dev dependencies vs build-time dependencies, etc. Docker is a level of isolation beyond this -- a Docker container is a completely different filesystem from your actual computer. The Dockerfile is a recipe that tells Docker how to build an image of your container, which Docker can run.

Basically, WORKDIR /usr/source/myapp creates a folder /usr/source/app in the Docker container's file system, and cd's into that for the rest of the Dockerfile. This means that the following line, COPY . ., will copy everything in the same folder as the Dockerfile into the folder in the container /usr/source/app.

I bet if you open a shell into the Docker container like so:

# Build the docker container
docker build . -t my-cool-project:latest

# Run it
docker run -it my-cool-project:latest bash

you should be able to cd /usr/source/app and see all your stuff.

Related