cargo build fails in Docker with Could not find directory of OpenSSL installation
Could not find directory of OpenSSL installationShort answer
The openssl crate links to the system OpenSSL. In a slim or Alpine build image there are no headers and no pkg-config, so openssl-sys cannot locate it. Install the development package and pkg-config in the build stage, or enable the vendored feature so cargo builds OpenSSL itself.
Fix it fast
1Find out which cause you have
Read the full build output above the error. Look for what is missing: pkg-config, the OpenSSL headers, or both.
docker build --progress=plain -t my-rust-app . 2>&1 | grep -B5 -A25 openssl-sys2Match what you see, then jump to its fix
- 1
The openssl-sys step fails on a slim or Alpine base imageThe build stage has no OpenSSL development files or pkg-configFix - 2
A cross-compile or a scratch or distroless final image, or no way to install packagesYou want a self-contained build with no system OpenSSLFix - 3
The files exist, but under a non-standard prefixOpenSSL is installed in a place the crate does not searchFix
- 1
What does the error look like?
Could not find directory of OpenSSL installationopenssl-sys build failure inside docker build
Cause 1 of 3
The build stage has no OpenSSL development files or pkg-config
1Check
On Unix-like systems openssl-sys uses pkg-config to find the system OpenSSL. A minimal image carries neither.
docker run --rm rust:slim sh -c 'which pkg-config; ls /usr/include/openssl | head'2Fix
Install the packages in the build stage, before cargo build.
# Debian or Ubuntu base
RUN apt-get update && apt-get install -y pkg-config libssl-dev \
&& rm -rf /var/lib/apt/lists/*
# Alpine base
RUN apk add pkgconf openssl-dev3Verify
Rebuild; the openssl-sys step now passes.
docker build -t my-rust-app .Confirmed by docs.rs
Cause 2 of 3
You want a self-contained build with no system OpenSSL
1Check
The vendored feature compiles and statically links its own OpenSSL copy, which needs a C compiler, Perl and make.
grep -n openssl Cargo.toml2Fix
Turn on the vendored feature in Cargo.toml and make sure the build image has perl and make.
# Cargo.toml
[dependencies]
openssl = { version = "0.10", features = ["vendored"] }3Verify
Rebuild the image.
docker build -t my-rust-app .Confirmed by docs.rs
Cause 3 of 3
OpenSSL is installed in a place the crate does not search
1Check
The crate reads OPENSSL_DIR, which must contain lib and include, or OPENSSL_LIB_DIR and OPENSSL_INCLUDE_DIR separately.
find / -name ssl.h -path '*openssl*' 2>/dev/null2Fix
Export the variable before cargo build.
ENV OPENSSL_DIR=/opt/openssl3Verify
Rebuild the image.
docker build -t my-rust-app .Confirmed by docs.rs
How do I stop it coming back?
- Keep the build-stage packages in one RUN line at the top of the Dockerfile.
- Match the library: a binary built against glibc OpenSSL will not run on an Alpine runtime stage.
- Use the vendored feature when you ship to scratch or distroless.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Prisma cannot find a query engine for debian-openssl in Docker
Query engine library for current platform "debian-openssl-1.1.x" could not be found
Prisma Client ships a query engine built for one OS and OpenSSL version.
2 causes, each with a fixServers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fix