Search code examples
linuxdockerubunturust

rust docker build got error: "cannot find -lssl: No such file or directory" on Ubuntu Server


i have try build on Ubuntu Server GCP, it was not successfully thought i already install musl-dev,

here is my Dockerfile

FROM rust:alpine AS builder

# Set the current working directory inside the Docker image
WORKDIR /usr/src

# Copy the Cargo.toml file and your source code into the Docker image
COPY Cargo.toml Cargo.lock ./
COPY src ./src
COPY secrets ./secrets
# Copy the dev-config.toml file
COPY dev-config.toml .

# Install build dependencies
RUN apk add --no-cache build-base libgcc libstdc++ openssl openssl-dev pkgconfig musl-dev pcc-libs-dev

# Build the application in release mode
RUN cargo build --release

# Start a new build stage with Alpine
FROM alpine:latest

# Install necessary libraries. libgcc and libstdc++, etc are needed for the Rust binary
RUN apk add --no-cache libgcc libstdc++ openssl pcc-libs-dev build-base  openssl-dev pkgconfig musl-dev

# Add a new user 'appuser'
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Copy the binary from the builder stage to the current stage
COPY --from=builder /usr/src/target/release/my-api /usr/local/bin

# Change to non-root privilege
USER appuser

# Set the command to run your application
CMD ["my-api"]

the error is like this

error: linking with `cc` failed: exit status: 1

= note: LC_ALL="C" PATH="/usr/local/rustup/toolchains/1.80.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/bin:/usr/local/rustup/toolchains/1.80.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/bin/self-contained:/usr/local/rustup/toolchains/1.80.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/bin:/usr/local/rustup/toolchains/1.80.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl.....
.....the rest

= note: /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lssl: No such file or directory
1099.5           /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lcrypto: No such file or directory
1099.5           collect2: error: ld returned 1 exit status

my os Ubuntu is on server to build that docker with version 24.04 LTS , 10 GB, and 2 GB RAM

is that something i need to install again on docker ? i have stuck for few days to build this docker into server,


Solution

  • I had the same issue trying to build a rust app in docker on my m2 Mac.

    I found this thread, and the user polarathene suggested importing openssl-libs-static as well and it worked for me.

    Hope it works for you too.

    These are my relevant apk imports:

    RUN apk add --no-cache openssl-dev
    
    RUN apk add --no-cache openssl-libs-static