Skip to content

Instantly share code, notes, and snippets.

@noteed
Last active September 24, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noteed/28dca40b79314a21daa81ddf7e3a2e39 to your computer and use it in GitHub Desktop.
Save noteed/28dca40b79314a21daa81ddf7e3a2e39 to your computer and use it in GitHub Desktop.
Two images
FROM alpine:3.12
ADD file-1.txt /
FROM alpine:3.12
COPY --from=image-1 /file-1.txt /
ADD file-2.txt /
CMD cat /file-1.txt /file-2.txt

This demonstrates how to use COPY --from in a Dockerfile to include arbitrary data from another image. For instance when building a Git repository, a Docker image can be used as a resulting artefact so some data from that repository can also be used in another image, built from another repository at a different time.

This file is originally from image-1.
This file is from image-2.
#! /usr/bin/env bash
# Build two images. The first one contains some file (e.g. obtained from a
# repository not available during the second image build).
# The second image is built with a COPY --from to obtain that file.
set -e
docker build -t image-1 -f 1.Dockerfile .
docker build -t image-2 -f 2.Dockerfile .
docker run image-2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment