skip to Main Content

If I have a very large file that I only need during build time, and If I use the following commands:

docker buildx build Dockerfile .

and then a line somewhere in the Dockerfile:

RUN --mount=type=bind,target=/target_path,readonly,source=large_dir_or_file

would that very large file still got send to the docker daemon? I am wondering if the –mount command offers any advantage here for the large files?

Also, if the docker context and daemon are on the same machine, are all the context files still sent/copied to a location where docker daemon can see them?

Thank you,
Daniel

2

Answers


  1. Even if you’re working locally, the docker build command always sends a copy of the context directory over its socket to the Docker daemon. If you have a very large build context (especially over a gigabyte in size) this is the step that prints out a percentage progress at the very beginning of the build sequence.

    The build context is everything in and underneath the directory you pass to docker build, less anything that’s in the .dockerignore file. This will always get sent to the Docker daemon, local or remote, whether or not any given file is actually COPYed into the image.

    I’m guessing the BuildKit bind-mount option you show probably will work mechanically. You need to make sure the large file is also in the .dockerignore file so it’s not copied as part of the build context. This will effectively prevent you from using a remote Docker daemon to build, if you or your CI system will ever do this, and it’s not a typical pattern, but it should have a visible difference in build performance and more specifically in that initial "copying the build context" step.

    You note in the question that this file is only used during your initial build sequence, and I’m guessing you copy the result of the build out using a multi-stage build so you have a much smaller image. My past experience has been that operations like docker push and docker pull are unreliable with very large images, so if you can’t remove this file from the final image you might need to inject it into the container in some other way.

    Login or Signup to reply.
  2. The advantage of

    RUN --mount=type=bind,target=/target_path,readonly,source=large_dir_or_file something
    

    over

    COPY large_dir_or_file /target_path
    RUN something
    

    is that the big file will not bloat the layers of your docker image.

    But yes, it must be in the context and it will be sent to the docker daemon even if it is local.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search