0

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?

Hugo Persson
  • 33
  • 1
  • 6
  • 1
    The error message indicates that you need zlib to run the compiled program, so you have to install zlib (and maybe some other libraries) inside the final container image. You'll also need to make sure that the versions of libraries in the builder image is compatible with the versions of libraries in the final image. – kotatsuyaki Dec 19 '22 at 07:23
  • @kotatsuyaki Do you know how I would install zlib in distroless? Would something like this work? ``` COPY --from=build /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1 ``` – Hugo Persson Dec 19 '22 at 07:30
  • I'm not familiar with distroless container images. While this does not directly solve your problem, what I normally do is to compile the program into a statically-linked executable under Alpine, which does not need any shared libraries. – kotatsuyaki Dec 19 '22 at 07:36
  • How would I do that? Do I need to supply any options to cargo build? @kotatsuyaki – Hugo Persson Dec 19 '22 at 10:09
  • You need to build the binary with `x86_64-unknown-linux-musl` as the target. See the answer under this question: [Rust docker image with small binary](https://stackoverflow.com/questions/72850392/rust-docker-image-with-small-binary) – kotatsuyaki Dec 19 '22 at 10:13

0 Answers0