Hello everyone,
I’ve encountered a persistent issue when trying to access the Rust toolchain (rustc
and cargo
) from within a Docker container, specifically when operating as a non-root user. Despite several attempts and strategies, I consistently face errors related to the Rust toolchain not being accessible or rustup
related errors when trying to run rustc --version
or cargo --version
I'm trying to build my application and use it for my github actions which i have set up on github.
Environment and Setup:
- Base Image: I start with a
rust:1.75.0
image to build my Rust application and then move to anubuntu
base image for the final setup. - Dockerfile Approach:
- In the
rust:1.75.0
image, I successfully compile my application and install necessary dependencies. - I then copy the compiled binary and Rust toolchain binaries (
/usr/local/cargo/bin/*
) to/usr/local/bin/
in theubuntu
image. - A non-root user (
userGeo
) is created, and I switch to this user before running any commands. - Despite setting
PATH
appropriately and ensuring permissions, I cannot accessrustc
orcargo
asuserGeo
. - As a root user i can only access cargo and rustc in the first image build, but when copied to my ubuntu image i no longer have access. the below error is all i get in all situations as none root user and a root user in the final image build which is ubuntu
- In the
no rustup userGeo@47******b:/usr/local/bin$ rustc --version
error: rustup could not choose a version of rustc to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
WORKDIR /src
COPY . .
RUN cargo build --locked --workspace
# Install sqlx-cli for database operations
RUN cargo install sqlx-cli --no-default-features --features native-tls,postgres
FROM ubuntu
# Update and install necessary packages including OpenSSL which sqlx-cli needs
RUN apt-get update && apt-get install -y libssl-dev pkg-config ca-certificates make curl build-essential && rm -rf /var/lib/apt/lists/*
# Create a new user 'userGeo' and group 'rustgroup'
RUN groupadd rustgroup && useradd -m -g rustgroup userGeo
# Set the HOME environment variable for userGeo
WORKDIR /home/userGeo
# Copy the sqlx-cli binary from the build stage
COPY --from=build /usr/local/cargo/bin/sqlx /usr/local/bin/sqlx
# Copy the rust toolchain from the build stage
COPY --from=build /usr/local/cargo/bin/* /usr/local/bin/
# Copy the compiled binary from the build stage to /usr/local/bin
COPY --from=build /src/target/debug/psp /usr/local/bin/psp
# Ensure 'userGeo' has the necessary permissions over their directories
RUN chown -R userGeo:rustgroup /home/userGeo
# Copy the entire /src directory to the working directory of userGeo in the Ubuntu image
COPY --from=build /src /home/userGeo
# Set the permissions so all users can execute the Rust binaries and give usergeo permissions
RUN chmod -R 755 /usr/local/bin/
# Switch to the non-root user for running the application
USER userGeo
CMD ["PSP"]```
above is my docker file. maybe i am doing it wrong. i have scanned through the web and i haven't been able to resolve this, any help would be super helpful