title: How to COPY library files between stages of a multi-stage Docker build while preserving symlinks?
body: <p>I have a Dockerfile which is split into a two-stage multi-stage docker build.  The first stage generates a basic gcc build environment in which a number of C and C++ library are compiled.  The second stage uses the <code>COPY --from=</code> command to copy the library files from the first stages <code>/usr/local/lib/libproto*</code> to the current image's.  </p>
  
  <p>The problem I am seeing is that the first image contains symlinks from a generic library file name to a specific versioned file name.  AFAIK this is common practice within Debian and many other Linux systems.  Docker's <code>COPY</code> command does not seem to understand symlinks so instead makes two complete copies of the library files.  This results in a larger Docker Image size and warnings from later <code>apt-get</code> commands to the tune of <code>ldconfig: /usr/local/lib/libprotobuf.so.17 is not a symbolic link</code>.  </p>
  
  <hr>
  
  <p>My specific file presently looks like:</p>
  
  <pre><code>#Compile any tools we cannot install from packages
  FROM gcc:7 as builder
  USER 0
  RUN \
    apt-get -y update &amp;&amp; \
    apt-get -y install \
      clang \
      libc++-dev \
      libgflags-dev \
      libgtest-dev
  RUN \
    # Protocol Buffer &amp; gRPC
    # install protobuf first, then grpc
    git clone -b $(curl -L https://grpc.io/release) \
        https://github.com/grpc/grpc /var/local/git/grpc &amp;&amp; \
      cd /var/local/git/grpc &amp;&amp; \
      git submodule update --init &amp;&amp; \
      echo "--- installing protobuf ---" &amp;&amp; \
      cd third_party/protobuf &amp;&amp; \
      ./autogen.sh &amp;&amp; ./configure --enable-shared &amp;&amp; \
      make -j$(nproc) &amp;&amp; make install &amp;&amp; make clean &amp;&amp; ldconfig &amp;&amp; \
      echo "--- installing grpc ---" &amp;&amp; \
      cd /var/local/git/grpc &amp;&amp; \
      make -j$(nproc) &amp;&amp; make install &amp;&amp; make clean &amp;&amp; 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 &amp;&amp; \
    apt-get -y install \
      libhdf5-dev \
      libssl1.1 \
      uuid-dev;
  </code></pre>
  
  <p>As you can see I am trying to add the latest versions of gRPC and Protocol Buffer to a Debian based runtime image.  </p>