Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get artifact out of docker build? [duplicate]

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.

like image 472
StoneThrow Avatar asked Nov 08 '20 21:11

StoneThrow


People also ask

Does Docker COPY overwrite existing file?

It seems that docker build won't overwrite a file it has previously copied. I have a dockerfile with several copy instructions, and files touched in earlier COPY directives don't get overwritten by later ones. After building this, $BASE/config/thatfile. yml contains the contents of file1.

What is a docker artifact?

Docker image artifacts are references to images in registries, such as GCR or Docker Hub. The artifacts can be deployed to Kubernetes or App Engine, and generally trigger pipelines from notifications sent by their registry.

Where is the output of Docker build?

They get stored as a series of layers in your Docker root directory. On Linux, it's /var/lib/docker .

Can you have multiple Dockerfiles?

Let's say we have two Dockerfiles, one for building the backend and another for building the frontend. We can name them appropriately and invoke the build command two times, each time passing the name of one of the Dockerfiles: $ docker build -f Dockerfile.


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.

like image 185
Christian Fosli Avatar answered Nov 15 '22 05:11

Christian Fosli