如何在保留符号链接的同时在多阶段Docker构建的各个阶段之间复制库文件?

时间:2018-11-13 13:00:07

标签: c++ docker shared-libraries dockerfile docker-multi-stage-build

我有一个Dockerfile,该文件分为两个阶段的多阶段docker构建。第一阶段生成一个基本的gcc构建环境,在该环境中编译了许多C和C ++库。第二阶段使用COPY --from=命令将库文件从第一阶段/usr/local/lib/libproto*复制到当前图像。

我看到的问题是第一张图像包含从通用库文件名到特定版本文件名的符号链接。 AFAIK这是Debian和许多其他Linux系统中的常见做法。 Docker的COPY命令似乎不了解符号链接,因此会制作两个完整的库文件副本。这会导致更大的Docker映像大小,并从后面的apt-get命令到ldconfig: /usr/local/lib/libprotobuf.so.17 is not a symbolic link的警告中发出警告。


我的特定文件目前看起来像:

#Compile any tools we cannot install from packages
FROM gcc:7 as builder
USER 0
RUN \
  apt-get -y update && \
  apt-get -y install \
    clang \
    libc++-dev \
    libgflags-dev \
    libgtest-dev
RUN \
  # Protocol Buffer & gRPC
  # install protobuf first, then grpc
  git clone -b $(curl -L https://grpc.io/release) \
      https://github.com/grpc/grpc /var/local/git/grpc && \
    cd /var/local/git/grpc && \
    git submodule update --init && \
    echo "--- installing protobuf ---" && \
    cd third_party/protobuf && \
    ./autogen.sh && ./configure --enable-shared && \
    make -j$(nproc) && make install && make clean && ldconfig && \
    echo "--- installing grpc ---" && \
    cd /var/local/git/grpc && \
    make -j$(nproc) && make install && make clean && ldconfig


FROM debian
LABEL \
 Description="Basic Debian production environment with a number of libraries configured" \
 MAINTAINER="Mr Me"
ARG prefix=/usr/local
ARG binPath=$prefix/bin
ARG libPath=$prefix/lib
# Copy over pre-made tools
# Protocol Buffer
COPY --from=builder /usr/local/lib/libproto* $libPath/
# gRPC
COPY --from=builder /usr/local/lib/libaddress_sorting.so.6.0.0 $libPath/
COPY --from=builder /usr/local/lib/libgpr* $libPath/
COPY --from=builder /usr/local/lib/libgrpc* $libPath/
RUN ldconfig
# Install remaining tools using apt-get
RUN apt-get -y update && \
  apt-get -y install \
    libhdf5-dev \
    libssl1.1 \
    uuid-dev;

如您所见,我正在尝试将最新版本的gRPC和Protocol Buffer添加到基于Debian的运行时映像中。

1 个答案:

答案 0 :(得分:2)

这不仅仅是解决方法,而不是答案。

您可以将文件打包,将tarball复制到第二个容器,然后解压缩它们。

Tar maintains symbolic links默认。