首页 > 解决方案 > 如何在多阶段 Docker 构建的阶段之间复制库文件,同时保留符号链接?

问题描述

我有一个 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 的运行时映像中。

标签: c++dockershared-librariesdockerfiledocker-multi-stage-build

解决方案


这更像是一种解决方法而不是答案。

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

Tar默认维护符号链接


推荐阅读