Search code examples
dockerrustlinker-errorsalpine-linux

How to fix "cannot find -lpq" error when using alpine linux docker image?


When I build my Rust application in Docker using alpine as the base image using this command:

docker build -f ./Dockerfile -t="reddwarf-pro/reddwarf-admin:v.1.0.0" .

It shows an error like this:

#14 222.9   = note: /usr/lib/gcc/aarch64-alpine-linux-musl/10.3.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lpq
#14 222.9           collect2: error: ld returned 1 exit status
#14 222.9
#14 222.9
#14 223.0 warning: `reddwarf-admin` (bin "reddwarf-admin") generated 1 warning
#14 223.0 error: could not compile `reddwarf-admin` due to previous error; 1 warning emitted
------
executor failed running [/bin/sh -c cargo build --release]: exit code: 101

This is the Dockerfile:

# to reduce the docker image size
# build stage
FROM rust:1.54-alpine as builder
WORKDIR /app
COPY . /app
RUN rustup default stable
RUN apk update && apk add --no-cache libpq musl-dev pkgconfig openssl-dev gcc
RUN cargo build --release
# RUN cargo build

# Prod stage
FROM alpine:3.15
WORKDIR /app
ENV ROCKET_ADDRESS=0.0.0.0
# ENV ROCKET_PORT=11014
RUN apk update && apk add --no-cache libpq curl
COPY --from=builder /app/.env /app
COPY --from=builder /app/settings.toml /app
COPY --from=builder /app/target/release/reddwarf-admin /app/
COPY --from=builder /app/Rocket.toml /app
CMD ["./reddwarf-admin"]

I have already added libpq in the alpine image. Why does it still show this error? I am also tried to add libpq-dev but it seems the alpine did not contains this library.

What should I do to fix this problem?


Solution

  • Perhaps slightly confusingly, the dev version is called postgresql-dev and not libpq-dev in v3.14. (From what I can tell, it was renamed libpq-dev in v3.15).

    libpq does indeed install the shared library, but postgresql-dev creates the symbolic link /usr/lib/libpq.so -> libpq.so.5.13 which makes linking succeed.

    From man ld:

    On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a ".so" extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

    That is, the -lname syntax will only search for libname.(a|so), not versioned names.