How to include files outside of Docker's build context?

前端 未结 14 811
情话喂你
情话喂你 2020-11-22 07:21

How can I include files from outside of Docker\'s build context using the \"ADD\" command in the Docker file?

From the Docker documentation:

T

相关标签:
14条回答
  • 2020-11-22 07:28

    You can also create a tarball of what the image needs first and use that as your context.

    https://docs.docker.com/engine/reference/commandline/build/#/tarball-contexts

    0 讨论(0)
  • 2020-11-22 07:29

    As is described in this GitHub issue the build actually happens in /tmp/docker-12345, so a relative path like ../relative-add/some-file is relative to /tmp/docker-12345. It would thus search for /tmp/relative-add/some-file, which is also shown in the error message.*

    It is not allowed to include files from outside the build directory, so this results in the "Forbidden path" message."

    0 讨论(0)
  • 2020-11-22 07:30

    I believe the simpler workaround would be to change the 'context' itself.

    So, for example, instead of giving:

    docker build -t hello-demo-app .
    

    which sets the current directory as the context, let's say you wanted the parent directory as the context, just use:

    docker build -t hello-demo-app ..
    
    0 讨论(0)
  • 2020-11-22 07:32

    The best way to work around this is to specify the Dockerfile independently of the build context, using -f.

    For instance, this command will give the ADD command access to anything in your current directory.

    docker build -f docker-files/Dockerfile .
    

    Update: Docker now allows having the Dockerfile outside the build context (fixed in 18.03.0-ce, https://github.com/docker/cli/pull/886). So you can also do something like

    docker build -f ../Dockerfile .
    
    0 讨论(0)
  • 2020-11-22 07:32

    On Linux you can mount other directories instead of symlinking them

    mount --bind olddir newdir
    

    See https://superuser.com/questions/842642 for more details.

    I don't know if something similar is available for other OSes. I also tried using Samba to share a folder and remount it into the Docker context which worked as well.

    0 讨论(0)
  • 2020-11-22 07:34

    An easy workaround might be to simply mount the volume (using the -v or --mount flag) to the container when you run it and access the files that way.

    example:

    docker run -v /path/to/file/on/host:/desired/path/to/file/in/container/ image_name
    

    for more see: https://docs.docker.com/storage/volumes/

    0 讨论(0)
提交回复
热议问题