dockerrustdockerfiledebianlibssl

Getting `error while loading shared libraries: libssl.so.1.1` when running docker container


I'm trying to deploy a rust server using docker, but when I try to run the container docker run myapp I get the following error: myapp: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory.

Here is my Dockerfile:


FROM rust:latest as builder

WORKDIR /usr/src/myapp

COPY . .

RUN cargo install --path .

FROM debian:bullseye
FROM postgres:latest 

ENV POSTGRES_USER user 
ENV POSTGRES_DB db

RUN apt-get update \ 
    && apt-get install -y libssl-dev \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/local/cargo/bin/myapp /usr/local/bin/myapp

CMD ["myapp"]

I'm new to Docker and would appreciate your help.

So far I have tried:

  1. Changing the debian base image from bullseye to bullseye-slim or latest
  2. Linking /usr/local/lib/libssl.so.1.1 to /usr/lib64/libssl.so.1.1

Solution

  • The problem is that you're building against a different OS than you're running against. Currently, the rust:latest target uses bullseye (Debian 11) as a base. Debian bullseye ships OpenSSL 1.1. However, postgres:latest uses bookworm (Debian 12) as a base, which ships OpenSSL 3.0. As a result, you have two different OSes with two different versions of OpenSSL, and your binary, which was linked against OpenSSL 1.1, can't find it when attempting to run.

    In general, unless you're statically linking (and even often then), you are almost always better off compiling and running on the same version of the same operating system, since that results in code that tends to be more likely to be functional and behave correctly.

    Note that your FROM debian:bullseye has no effect, since it's immediately overridden by FROM postgres:latest.

    If you want to use bookworm for both, then specify FROM rust:bookworm (and, ideally, FROM postgres:bookworm). If you want to use bullseye for both, then use FROM rust:bullseye and FROM postgres:bullseye. That will make sure that your code builds and runs on the same platform. You also don't need to install libssl-dev to run the code, only either libssl1.1 for bullseye or libssl3 for bookworm.