0

I have persisting issue when I try to build Rust in docker. It used to work but recently started to break. I've tried different images but still having this error. It seems obvious that libssl-dev should be installed but I'm already doing this. Please, any clue how to fix this?

This is docker file

FROM rust:slim AS builder

RUN apt-get update -y && \
  apt-get install -y make g++ libssl-dev && \
  rustup target add x86_64-unknown-linux-gnu

WORKDIR /app
COPY . .

RUN cargo build --release --target x86_64-unknown-linux-gnu


FROM gcr.io/distroless/cc
COPY --from=builder /app/target/x86_64-unknown-linux-gnu/release/rebot /bin/rebot
ENTRYPOINT [ "/bin/rebot" ]

This is the error.

#11 426.3 error: failed to run custom build command for `openssl-sys v0.9.80`
#11 426.3
#11 426.3 Caused by:
#11 426.3   process didn't exit successfully: `/app/target/release/build/openssl-sys-a00b6be9953405cd/build-script-main` (exit status: 101)
#11 426.3   --- stdout
#11 426.3   cargo:rustc-cfg=const_fn
#11 426.3   cargo:rustc-cfg=openssl
#11 426.3   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR
#11 426.3   X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR unset
#11 426.3   cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
#11 426.3   OPENSSL_LIB_DIR unset
#11 426.3   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR
#11 426.3   X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR unset
#11 426.3   cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
#11 426.3   OPENSSL_INCLUDE_DIR unset
#11 426.3   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR
#11 426.3   X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR unset
#11 426.3   cargo:rerun-if-env-changed=OPENSSL_DIR
#11 426.3   OPENSSL_DIR unset
#11 426.3   cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-gnu
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_gnu
#11 426.3   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
#11 426.3   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
#11 426.3   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
#11 426.3   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
#11 426.3   run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."
#11 426.3
#11 426.3   --- stderr
#11 426.3   thread 'main' panicked at '
#11 426.3
#11 426.3   Could not find directory of OpenSSL installation, and this `-sys` crate cannot
#11 426.3   proceed without this knowledge. If OpenSSL is installed and this crate had
#11 426.3   trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
#11 426.3   compilation process.
#11 426.3
#11 426.3   Make sure you also have the development packages of openssl installed.
#11 426.3   For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
#11 426.3
#11 426.3   If you're in a situation where you think the directory *should* be found
#11 426.3   automatically, please open a bug at https://github.com/sfackler/rust-openssl
#11 426.3   and include information about your system as well as this message.
#11 426.3
#11 426.3   $HOST = aarch64-unknown-linux-gnu
#11 426.3   $TARGET = x86_64-unknown-linux-gnu
#11 426.3   openssl-sys = 0.9.80
#11 426.3
#11 426.3
#11 426.3   It looks like you're compiling on Linux and also targeting Linux. Currently this
#11 426.3   requires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config`
#11 426.3   could not be found. If you have OpenSSL installed you can likely fix this by
#11 426.3   installing `pkg-config`.
#11 426.3
#11 426.3   ', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.80/build/find_normal.rs:191:5
#11 426.3   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#11 426.3 warning: build failed, waiting for other jobs to finish...
------
executor failed running [/bin/sh -c cargo build --release --target x86_64-unknown-linux-gnu]: exit code: 101

Update 1: Adding dependencies in cargo

openssl-sys = "0.9.82"
openssl = { version = "0.10.47", features = ["vendored"] }

Gives this error

#14 93.18   make[1]: Leaving directory '/rebot/target/x86_64-unknown-linux-musl/release/build/openssl-sys-5963a97b9196664b/out/openssl-build/build/src'
#14 93.18
#14 93.18   --- stderr
#14 93.18   cc1: error: unrecognized command-line option '-m64'
#14 93.18   cc1: error: unrecognized command-line option '-m64'
#14 93.18   make[1]: *** [Makefile:679: apps/app_rand.o] Error 1
#14 93.18   make: *** [Makefile:177: build_libs] Error 2
#14 93.18   thread 'main' panicked at '
#14 93.18
#14 93.18
#14 93.18   Error building OpenSSL:
#14 93.18       Command: cd "/rebot/target/x86_64-unknown-linux-musl/release/build/openssl-sys-5963a97b9196664b/out/openssl-build/build/src" && MAKEFLAGS="-j --jobserver-fds=9,14 --jobserver-auth=9,14" "make" "build_libs"
#14 93.18       Exit status: exit status: 2
#14 93.18
#14 93.18
#14 93.18       ', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/openssl-src-111.25.2+1.1.1t/src/lib.rs:505:13
#14 93.18   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#14 93.18 warning: build failed, waiting for other jobs to finish...
Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46

1 Answers1

1

You need to install pkg-config as well, as this is used to detect the existing OpenSSL installation. See also https://docs.rs/openssl/latest/openssl/. Tested as follows:

$ cargo new rebot
$ cd rebot
$ cargo add openssl-sys

Dockerfile

FROM rust:slim AS builder

RUN apt-get update -y && \
  apt-get install -y pkg-config make g++ libssl-dev && \
  rustup target add x86_64-unknown-linux-gnu

WORKDIR /app
COPY . .

RUN cargo build --release --target x86_64-unknown-linux-gnu


FROM gcr.io/distroless/cc
COPY --from=builder /app/target/x86_64-unknown-linux-gnu/release/rebot /bin/rebot
ENTRYPOINT [ "/bin/rebot" ]

Output

$ docker build . -t rebot
[+] Building 46.5s (13/13) FINISHED                                                                                                                           
 => [internal] load .dockerignore                                                                                                                        0.1s
 => => transferring context: 2B                                                                                                                          0.0s
 => [internal] load build definition from Dockerfile                                                                                                     0.1s
 => => transferring dockerfile: 417B                                                                                                                     0.0s
 => [internal] load metadata for gcr.io/distroless/cc:latest                                                                                             0.5s
 => [internal] load metadata for docker.io/library/rust:slim                                                                                             0.9s
 => [builder 1/5] FROM docker.io/library/rust:slim@sha256:dd8928e7009f8d48273f1e0fb8e050f73f8d16aaa928b624319d86f583c17188                               0.0s
 => CACHED [stage-1 1/2] FROM gcr.io/distroless/cc@sha256:fb402c45f3ef485ccd56ca2af2a58615fc47c4978bb3004e8663a83456791f48                               0.0s
 => [internal] load build context                                                                                                                        0.1s
 => => transferring context: 1.84kB                                                                                                                      0.0s
 => CACHED [builder 2/5] RUN apt-get update -y &&   apt-get install -y pkg-config make g++ libssl-dev &&   rustup target add x86_64-unknown-linux-gnu    0.0s
 => CACHED [builder 3/5] WORKDIR /app                                                                                                                    0.0s
 => [builder 4/5] COPY . .                                                                                                                               0.2s
 => [builder 5/5] RUN cargo build --release --target x86_64-unknown-linux-gnu                                                                           44.1s
 => [stage-1 2/2] COPY --from=builder /app/target/x86_64-unknown-linux-gnu/release/rebot /bin/rebot                                                      0.2s
 => exporting to image                                                                                                                                   0.7s 
 => => exporting layers                                                                                                                                  0.7s 
 => => writing image sha256:2f72c6955bc946d23326685bd48885186c9ab4aa194cf2267b9d6a6423aac1a1                                                             0.0s 
 => => naming to docker.io/library/rebot        
$ docker run rebot
Hello, world!
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118