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:
- Changing the debian base image from
bullseyetobullseye-slimorlatest - Linking
/usr/local/lib/libssl.so.1.1to/usr/lib64/libssl.so.1.1
The problem is that you're building against a different OS than you're running against. Currently, the
rust:latesttarget uses bullseye (Debian 11) as a base. Debian bullseye ships OpenSSL 1.1. However,postgres:latestuses 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:bullseyehas no effect, since it's immediately overridden byFROM 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 useFROM rust:bullseyeandFROM postgres:bullseye. That will make sure that your code builds and runs on the same platform. You also don't need to installlibssl-devto run the code, only eitherlibssl1.1for bullseye orlibssl3for bookworm.