Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files from dockerfile to host?

I want to get some files when dockerfile built successfully, but it doesn't copy files from container to host.

That means when I have built dockerfile, the files already be host.

like image 212
Allen Avatar asked Oct 27 '15 19:10

Allen


People also ask

How do I COPY a file from Docker to hosting machine?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.


2 Answers

This is now possible since Docker 19.03.0 in July 2019 introduced "custom build outputs". See the official docs about custom build outputs.

To enable custom build outputs from the build image into the host during the build process, you need to activate the BuildKit which is a newer recommended back-compatible way for the engine to do the build phase. See the official docs for enabling BuildKit.

This can be done in 2 ways:

  1. Set the environment variable DOCKER_BUILDKIT=1, or
  2. Set it in the docker engine by default by adding "features": { "buildkit": true } to the root of the config json.

From the official docs about custom build outputs:

custom exporters allow you to export the build artifacts as files on the local filesystem instead of a Docker image, which can be useful for generating local binaries, code generation etc.

...

The local exporter writes the resulting build files to a directory on the client side. The tar exporter is similar but writes the files as a single tarball (.tar).

If no type is specified, the value defaults to the output directory of the local exporter.

...

The --output option exports all files from the target stage. A common pattern for exporting only specific files is to do multi-stage builds and to copy the desired files to a new scratch stage with COPY --from.

e.g. an example Dockerfile

FROM alpine:latest AS stage1 WORKDIR /app RUN echo "hello world" > output.txt  FROM scratch AS export-stage COPY --from=stage1 /app/output.txt . 

Running

DOCKER_BUILDKIT=1 docker build --file Dockerfile --output out . 

The tail of the output is:

 => [export-stage 1/1] COPY --from=stage1 /app/output.txt . 0.0s  => exporting to client 0.1s  => => copying files 45B 0.1s 

This produces a local file out/output.txt that was created by the RUN command.

$ cat out/output.txt hello world 

All files are output from the target stage

The --output option will export all files from the target stage. So using a non-scratch stage with COPY --from will cause extraneous files to be copied to the output. The recommendation is to use a scratch stage with COPY --from.

like image 178
Ben T Avatar answered Oct 08 '22 21:10

Ben T


Copying files "from the Dockerfile" to the host is not supported. The Dockerfile is just a recipe specifying how to build an image.

When you build, you have the opportunity to copy files from host to the image you are building (with the COPY directive or ADD)

You can also copy files from a container (an image that has been docker run'd) to the host with docker cp (atually, the cp can copy from the host to the container as well)

If you want to get back to your host some files that might have been generated during the build (like for example calling a script that generates ssl), you can run a container, mounting a folder from your host and executing cp commands.

See for example this getcrt script.

docker run -u root --entrypoint=/bin/sh --rm -i -v ${HOME}/b2d/apache:/apache apache << COMMANDS pwd cp crt /apache cp key /apache echo Changing owner from \$(id -u):\$(id -g) to $(id -u):$(id -u) chown -R $(id -u):$(id -u) /apache/crt chown -R $(id -u):$(id -u) /apache/key COMMANDS 

Everything between COMMANDS are commands executed on the container, including cp ones which are copying on the host ${HOME}/b2d/apache folder, mounted within the container as /apache with -v ${HOME}/b2d/apache:/apache.

That means each time you copy anything on /apache in the container, you are actually copying in ${HOME}/b2d/apache on the host!

like image 35
VonC Avatar answered Oct 08 '22 22:10

VonC