Copying things out of a Docker based build

I have a Dockerfile that produces some build artifacts.

I’m struggling to find an example or documentation in this case where I need to copy files out of the build Dockerfile.

Does someone have an example I could please refer to?

Thank you in advance!

With Docker in general, the following ways to get files out of a container come to mind:

  • Run the docker cp command

    Copy files/folders between a container and the local filesystem

    This would need to be run on the host system, however.

  • Mount a folder of the host system into the container as a volume, then copy files to the mounted folder inside the container.

  • Use a multi-stage Docker image to only leave the files you are interested in in the container at the end of the image building process. If you want to copy out files when running this image later, then this option is irrelevant.

    FROM <base-image> AS build
    FROM scratch AS artifacts
    COPY --from=build /path/to/file /

    Afterwards, you can save the image as archive with docker save -o artifacts.tar <image-name> and unpack it to get at the contents. Note that there’s a bit more unpacking to do, because Docker saves metadata and all the layers individually (here it is just one layer because of the scratch image, however).

  • Store the build artifacts externally, e.g. by uploading them to a cloud storage provider.

Do you use your Docker image via jobs.<job_id>.container.image or do you run it like docker run ... in a workflow step?

In the former case, use the volume key to mount a folder into the container:

In the latter case, you would also need to do that, but by manually mounting a volume with -v in the docker run command.

1 Like