Home > Blockchain >  Docker container exit with error code error libcurl not found
Docker container exit with error code error libcurl not found

Time:12-04

I am building a container, you can see the docker file, its for rust app deployment on Argonaut. but its not able to start. Here you can see the Dockerfile.

FROM rust:1.64.0-buster AS builder
WORKDIR /app

ARG TOKEN
ARG DATABASE_URL

RUN git config --global url."https://${TOKEN}:@github.com/".insteadOf "https://github.com/"

COPY . .

ENV CARGO_NET_GIT_FETCH_WITH_CLI true

RUN rustup component add rustfmt
RUN apt-get update -y && apt-get install git wget ca-certificates curl gnupg lsb-release cmake libcurl4 -y

RUN cargo build

FROM debian:buster-slim
WORKDIR /app
COPY --from=builder /app/target/debug/linkedin /app/target/release/linkedin
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

CMD ["/app/target/release/linkedin"]
EXPOSE 3000

It builds successfully but when it works it gets exit with error code 127.

linkedin-leadr-1  | /app/target/release/linkedin: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

Have not found what's wrong with it, even though I am installing libcurl4. but my docker container is not able to find it. Can you please give me the solution?

CodePudding user response:

It looks like your Dockerfile is missing a step to copy the libcurl shared library into your container. This is why you are getting the "error while loading shared libraries" message when you try to run the container.

To fix this, you can add a step to your Dockerfile to copy the libcurl shared library from the builder stage into the final container. Here's an example of how you might do this:

FROM rust:1.64.0-buster AS builder
WORKDIR /app

ARG TOKEN
ARG DATABASE_URL

RUN git config --global url."https://${TOKEN}:@github.com/".insteadOf "https://github.com/"

COPY . .

ENV CARGO_NET_GIT_FETCH_WITH_CLI true

RUN rustup component add rustfmt
RUN apt-get update -y && apt-get install git wget ca-certificates curl gnupg lsb-release cmake libcurl4 -y

RUN cargo build

# Copy the libcurl shared library from the builder stage into the final container
RUN mkdir -p /usr/local/lib && \
    cp /usr/lib/x86_64-linux-gnu/libcurl.so.4 /usr/local/lib && \
    ln -s /usr/local/lib/libcurl.so.4 /usr/local/lib/libcurl.so

FROM debian:buster-slim
WORKDIR /app
COPY --from=builder /app/target/debug/linkedin /app/target/release/linkedin
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

CMD ["/app/target/release/linkedin"]
EXPOSE 3000

You will need to adjust the path to the libcurl shared library based on your system and the version of libcurl that you are using. The example above assumes that you are running on a 64-bit Linux system and using libcurl version 4.

After making this change and rebuilding your container, you should be able to run it without encountering the "error while loading shared libraries" message.

CodePudding user response:

As you install libcurl4 in your build environment but not in your execution environment, that's most likely the reason.

There are two ways to solve this:

  • Install libcurl4 in your final image, or
  • Link statically by replacing cargo build with
    RUN rustup target add x86_64-unknown-linux-musl
    RUN cargo build --target=x86_64-unknown-linux-musl --release
    

The --release flag should get added either way, as I'm sure you don't want to deliver unoptimized debug builds to your enduser ;)

Note that if you choose to install libcurl4 in your final image, you need to clean up the apt cache afterwards, otherwise your image grows immensely:

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --yes \
    libcurl4 \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
  • Related