Encountering error while trying to dockerize Rust app in multiple steps

578 views Asked by At

When I try to put my rust app in a Docker container with multiple build steps to reduce size I get the following error:

error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

My Cargo.toml is the following:

[package]
name = "communcation_service"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
serde = "1.0"
lapin = "2.1.1" # Kinda big
futures-lite = "1.12.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.16" # Kinda big
dotenv = "0.15.0"
base64 = "0.13.1"
async-trait = "0.1.58"
web-push = "0.9.3"
sentry = "0.23.0"
lettre = { version = "0.10.0-beta.2", default-features = false, features = ["smtp-transport", "tokio1-rustls-tls", "hostname", "r2d2", "builder"] }

And my Dockerfile is the following

FROM rust as build

WORKDIR /app

RUN cargo init .
RUN ls && rm ./Cargo.toml


# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml

# this build step will cache your dependencies
RUN cargo build --release
RUN rm src/*.rs

# copy your source tree
COPY ./src ./src

# build for release
RUN rm ./target/release/deps/communcation_service*
RUN cargo build --release
#CMD ["./target/release/communcation_service"]
# our final base
FROM gcr.io/distroless/cc-debian10
#FROM ubuntu


WORKDIR /app


# copy the build artifact from the build stage
COPY --from=build /app/target/release/communcation_service /app



# set the startup command to run your binary
CMD ["./communcation_service"]

My program works if I do not use multiple build steps and instead run the program in the build container, with this Dockerfile

FROM rust as build

WORKDIR /app

RUN cargo init .
RUN ls && rm ./Cargo.toml


# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml

# this build step will cache your dependencies
RUN cargo build --release
RUN rm src/*.rs

# copy your source tree
COPY ./src ./src

# build for release
RUN rm ./target/release/deps/communcation_service*
RUN cargo build --release
CMD ["./target/release/communcation_service"]

Do anyone know how to solve my error while still running the application in a smaller container?

0

There are 0 answers