8

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.

StoneThrow
  • 5,314
  • 4
  • 44
  • 86
  • 1
    Very current versions of Docker appear to have a way to dump the built image; the linked question has the syntax. It may help your "update" use case to know that the 12-digit hex IDs that `docker build` prints out along the way are valid Docker image IDs, so you can `docker run --rm -it 5a440f258772 sh` to get an interactive debugging shell on a partially-built image. – David Maze Nov 09 '20 at 01:02
  • @DavidMaze - confirming what you explained: I can indeed do `docker run --rm -it` of those "intermediate" docker images, and get my file that way. Thank you! – StoneThrow Nov 09 '20 at 19:23

1 Answers1

2

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.

Christian Fosli
  • 1,646
  • 9
  • 14
  • 1
    Unfortunately, my specific problem is that my Dockerfile runs executables that fail (return status is nonzero), therefore a docker image is not generated, and therefore I don't have an image to run as a container. Therefore I was trying to get debug information from the `docker build` process itself. Sorry: my intent was to simplify the scope of my question, but I guess that was pertinent information. I will update the question. – StoneThrow Nov 09 '20 at 00:32
  • Aah, that makes sense, no worries :-) – Christian Fosli Nov 09 '20 at 06:29