8

During build time, I want to copy a file from the image (from folder /opt/myApp/myFile.xml), to my host folder /opt/temp In the Dockerfile, I'm using the --mount as follows, trying to mount to my local test folder:

RUN --mount=target=/opt/temp,type=bind,source=test cp /opt/myApp/myFile.xml /opt/temp

I'm building the image successfully, but the local test folder is empty any ideas?

3
  • BTW when I print the destination folder, I can see the file there. 'RUN echo $(ls -1 /opt/temp)' The issue is with the mounting. nothing is seen on the test folder on local host.
    – Mary1
    Aug 4, 2020 at 8:14
  • you've the COPY directives that you can use in Dockerfile. #COPY-Dockerfile
    – vijay v
    Aug 4, 2020 at 9:23
  • @vijay, The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>. This question is the other way around: from the image to the host. Aug 4, 2020 at 9:30

1 Answer 1

6

Copying files from the image to the host at build-time is not supported.
This can easily be achieved during run-time using volumes.

However, if you really want to work-around this by all means, you can have a look in the custom build outputs documentation, that introduced support for this kind of activity.

Here is a simple example inspired from the official documentation:

Dockerfile

FROM alpine AS stage-a
RUN mkdir -p /opt/temp/
RUN touch /opt/temp/file-created-at-build-time
RUN echo "Content added at build-time" > /opt/temp/file-created-at-build-time

FROM scratch as custom-exporter
COPY --from=stage-a /opt/temp/file-created-at-build-time .

For this to work, you need to launch the build command using these arguments:

DOCKER_BUILDKIT=1 docker build --output out .

This will create on your host, aside the Dockerfile, a directory out with the file you need:

.
├── Dockerfile
└── out
    └── file-created-at-build-time
cat out/file-created-at-build-time 
Content added at build-time

3
  • now I notice that the Custom build output is instead of a Docker image. I need both, the image and the output file. Can this be achieved?
    – Mary1
    Aug 10, 2020 at 12:43
  • docker build --target stage-a -t stage-a-image-name. This will build only stage-a, will create an image from stage-a and skip stage custom-exporter. I don't think you can get both outputs in one shot. Aug 10, 2020 at 13:09
  • is there a way to close/end the custom-exporter stage? I have more code after this stage, that must be applied in the end. I need the last part to be part of my image: FROM alpine AS stage-a RUN mkdir -p /opt/temp/ RUN touch /opt/temp/file-created-at-build-time RUN echo "Content added at build-time" > /opt/temp/file-created-at-build-time FROM scratch as custom-exporter COPY --from=stage-a /opt/temp/file-created-at-build-time . CMD /bin/bash myscript.sh
    – Mary1
    Aug 12, 2020 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.