router icon indicating copy to clipboard operation
router copied to clipboard

Custom router build requires additional shared object. How can I author a dockerfile to build a usable docker image?

Open hboylan opened this issue 3 years ago • 3 comments

Describe the solution you'd like I generated a custom binary using the docs. Now, I am unable to build a custom Dockerfile using the provided example.

Describe alternatives you've considered When I build the Dockerfile, it returns the error:

./router: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

Additional context Here is a copy of my current Dockerfile:

# Reference: https://github.com/apollographql/router/blob/main/dockerfiles/diy/dockerfiles/Dockerfile.repo

# Use the rust build image from docker as our base
FROM --platform=arm64 rust:1.62.1 as build

# Set our working directory for the build
WORKDIR /router

# Update our build image and install required packages
RUN apt-get update
RUN apt-get -y install \
    npm \
    nodejs \
    libcurl4-openssl-dev

# Add rustfmt since build requires it
RUN rustup component add rustfmt

# Copy the router source to our build environment
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
COPY ./src ./src
COPY ./xtask ./xtask

# Build cache version
RUN cargo build --release
RUN rm src/*.rs

# copy your source tree
COPY ./src ./src

# Build custom binary for release
RUN rm ./target/release/deps/router*
RUN cargo build --release

# Copy configuration for docker image
RUN mkdir -p /dist/config
COPY /router.yaml /dist/config/router.yaml

# Required so we can copy in libz.so.1
FROM --platform=linux/amd64 gcr.io/distroless/java17-debian11 as libz-required

# Final image uses distroless. Feel free to change this to an image that suits your needs.
FROM --platform=linux/amd64 gcr.io/distroless/cc-debian11

# Set a label for our image
LABEL org.opencontainers.image.authors="Shipt graphql-router"

# Copy in the required files from our build image
COPY --from=libz-required --chown=root:root /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1
COPY --from=build --chown=root:root /dist /dist
COPY --from=build --chown=root:root /router/target/release/router /dist

# copy static shell into base image.
COPY --from=busybox:latest /bin/sh /bin/sh
COPY --from=busybox:latest /bin/ls /bin/ls

WORKDIR /dist

ENV APOLLO_ROUTER_CONFIG_PATH="/dist/config/router.yaml"

# Default executable is the router
ENTRYPOINT ["./router"]

hboylan avatar Aug 11 '22 17:08 hboylan

I'm going to re-phrase this a little, to better represent the actual problem.

Also: it looks like your custom binary requires the shared library libcurl.so.4. If that is present in the base java17 image, then you can copy it in a similar fashion to how libz.so.1 is copied. If not, you'll need to figure out a way to get that shared object into your docker image. That may involve using a different base image to distroless.

garypen avatar Aug 11 '22 17:08 garypen

Perfect, thank you. That summarizes the issue. I thought libcurl.so.4 might be required by the base apollo-router, but it may actually be required by something in my plugin so I will verify that...

hboylan avatar Aug 11 '22 18:08 hboylan

I'm fairly sure we don't require libcurl.so.4 in the router. Most likely suspect would be if you used the curl crate in your plugin.

garypen avatar Aug 11 '22 18:08 garypen

@hboylan Have you made any progress on this? I had a think about it and it may be that you can copy the required libcurl.so.4 from your build image into your final image.

Something like:

COPY --from=build --chown=root:root /usr/lib/<arch specific bit>/libcurl.so.4 /usr/lib/<arch specific bit>/libcurl.so.4

as an additional COPY line in your dockerfile.

I'd like to close this isssue, if you agree, because I don't think it's a router issue as such.

garypen avatar Aug 18 '22 08:08 garypen

Thank you for checking. I haven't been able to locate the library to copy from the build image. If you think this is a dependency of another dependency then we can close the issue.

The libz copy made me think another might be missing from the example is all.

hboylan avatar Aug 18 '22 13:08 hboylan