1

I have the following snippet in my Dockerfile:

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN USER=root cargo new --bin foobar

The problem is, the 2nd RUN fails because cargo is not in the path.

Is there a way to fix this?

I've tried setting the path like this (inspired by this answer):

ENV PATH $HOME/.cargo/.bin:$PATH

but this doesn't seem to do the trick

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • Why not `FROM` one of the `rustlang/rust` images: https://hub.docker.com/_/rust. Then you've got Rust installed and you're off to the races! – DazWilkin Feb 27 '21 at 01:18
  • I'm compiling on this image: https://hub.docker.com/r/pytorch/pytorch to ensure the resulting rust binary doesn't have dynamic linking issues – Foobar Feb 27 '21 at 01:20
  • Ah, OK. You may want to check the rustlang Dockerfile to see how they do it: https://github.com/rust-lang/docker-rust/blob/master/Dockerfile-debian.template – DazWilkin Feb 27 '21 at 01:34
  • You have a typo in the path to cargo binary. You wrote: `ENV PATH $HOME/.cargo/.bin:$PATH` Should drop the last dot to `ENV PATH $HOME/.cargo/bin:$PATH` – peralmq Jun 28 '22 at 11:24

1 Answers1

1

You can use --from with COPY command.

FROM rust:1.40 as builder
WORKDIR /usr/src/myapp
COPY . .
RUN cargo new --bin foobar

FROM another-image-that-you-want-to-use
COPY --from=builder /usr/local/cargo/bin/myapp /usr/local/bin/myapp
...
Mustafa Güler
  • 884
  • 6
  • 11