How to get artifact out of docker build?

Viewed 4612

Is there a way to get artifacts from docker build out to the host machine?

As an example, given the following Dockerfile, is there a way to get the file log.log?

FROM alpine:3.7 as base
RUN mkdir /log/
RUN touch /log/log.log

My attempt at using COPY seems to only work copying from host to docker image:

FROM alpine:3.7 as base
RUN mkdir /log/
RUN touch /log/log.log
COPY ./foo.bar /log/
RUN ls -l /log/
COPY /log/log.log ./
$ touch ./foo.bar && docker build -t demo --no-cache .
Sending build context to Docker daemon  15.36kB
Step 1/6 : FROM alpine:3.7 as base
 ---> 6d1ef012b567
Step 2/6 : RUN mkdir /log/
 ---> Running in 4b6df3797ee3
Removing intermediate container 4b6df3797ee3
 ---> 827e6001d34a
Step 3/6 : RUN touch /log/log.log
 ---> Running in d93d50d61b69
Removing intermediate container d93d50d61b69
 ---> c44620d4f9c4
Step 4/6 : COPY ./foo.bar /log/
 ---> 6996718d44da
Step 5/6 : RUN ls -l /log/
 ---> Running in 84e997af182b
total 0
-rw-r--r--    1 root     root             0 Nov  8 21:44 foo.bar
-rw-r--r--    1 root     root             0 Nov  8 21:44 log.log
Removing intermediate container 84e997af182b
 ---> 5a440f258772
Step 6/6 : COPY /log/log.log ./
COPY failed: stat /var/lib/docker/tmp/docker-builder677155266/log/log.log: no such file or directory

I'm aware of the -v (volume mount) argument to docker run - but that's not what I'm looking for: I'm looking to learn if there's a way to get artifacts from out of the docker build process specifically.


Update: RE: @ChristianFosli's suggestion to use docker cp: that solution requires a docker container. However, in my case, the reason I am looking to extract files specifically during the docker build process is that my Dockerfile runs an executable that fails, therefore I have no image that I can run as a container on which I can perform the docker cp. I.e. the file that I would like to extract from the docker build process is a metadata file that contains debugging information about the failure, that I'd like to inspect.

1 Answers

You can copy files from a container to your local filesystem using docker cp.

Check the docs for details

I don't know of a way to copy files directly from a docker image, but you can create a container based on your image, without running it, with docker create, and then copy files from it with docker cp.

Related