StoneThrow
StoneThrow

Reputation: 6255

How do I copy files from from the current docker build stage from one directory to another?

How is one meant to copy files that are part of the base image of the current stage in a multistage docker build?

E.g. if I want to start with a base image of alpine 3.7 how would I copy the file /etc/resolv.conf to somewhere I wanted it?


First version of my Dockerfile:

# Dockerfile
FROM alpine:3.7 as dev
WORKDIR /test
COPY /etc/resolv.conf /test
$ docker build -t foo:bar .
Sending build context to Docker daemon  14.85kB
Step 1/3 : FROM alpine:3.7 as dev
 ---> 6d1ef012b567
Step 2/3 : WORKDIR /test
 ---> Using cache
 ---> a82a71a856b0
Step 3/3 : COPY /etc/resolv.conf /test
COPY failed: stat /var/lib/docker/tmp/docker-builder820934750/etc/resolv.conf: no such file or directory

Second version of my Dockerfile:

# Dockerfile
FROM alpine:3.7 as dev
WORKDIR /test
COPY --from=dev /etc/resolv.conf /test
$ docker build -t foo:bar .
Sending build context to Docker daemon  14.85kB
Step 1/3 : FROM alpine:3.7 as dev
 ---> 6d1ef012b567
Step 2/3 : WORKDIR /test
 ---> Using cache
 ---> a82a71a856b0
Step 3/3 : COPY --from=dev /etc/resolv.conf /test
invalid from flag value dev: pull access denied for dev, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

Upvotes: 0

Views: 4660

Answers (1)

tadman
tadman

Reputation: 211540

Creating a multi-stage build can be tricky to get right, but the basics look like this:

FROM alpine:3.7 as dev
WORKDIR /test

# ... Do additional stuff in the "dev" stage

# Define a new stage called whatever you want, here called "final"
FROM alpine:3.7 as final

# Copy from an earlier stage
COPY --from=dev /etc/resolv.conf /test

Doing --from only makes sense if you are referencing a previous stage. It doesn't do anything useful if you have only one stage. It cannot reference other Dockerfile stages directly, you must have a corresponding FROM ... AS ... directive.

Upvotes: 2

Related Questions