The Power of Docker’s COPY — from

Philip Hutson
2 min readApr 20, 2022

I started working on a posting on multi-stage build after seeing a recent post about trying to reduce their container size with no mention of multi-stage builds. I was going to try and highlight the power of Docker’s COPY — from in that post but figured it would get lost.

In the Docker documents it says

Optionally COPY accepts a flag — from=<name> that can be used to set the source location to a previous build stage (created with FROM .. AS <name>) that will be used instead of a build context sent by the user. In case a build stage with a specified name can’t be found an image with the same name is attempted to be used instead.

The final clause in that statement:

In case a build stage with a specified name can’t be found an image with the same name is attempted to be used instead.

is what I want to highlight. With that feature in the implementation of the COPY a docker file can pull in files from any accessible docker image.

To illustrate that feature I created a simple GitHub repo with a Dockerfile where it pulled in a file from an image that was not on my system (I did a docker system prune). Then during the run it reported resolving and retrieving the image from docker.io and then grabbing the file.

The examples can be seen at

This provides another option for a data store that can be retrieved from during a build.

Important Points:

COPY — from

  • Can copy from previous build stages
  • Can copy from any retrievable docker image

--

--