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

前端 未结 14 849
情话喂你
情话喂你 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:44

    One quick and dirty way is to set the build context up as many levels as you need - but this can have consequences. If you're working in a microservices architecture that looks like this:

    ./Code/Repo1
    ./Code/Repo2
    ...
    

    You can set the build context to the parent Code directory and then access everything, but it turns out that with a large number of repositories, this can result in the build taking a long time.

    An example situation could be that another team maintains a database schema in Repo1 and your team's code in Repo2 depends on this. You want to dockerise this dependency with some of your own seed data without worrying about schema changes or polluting the other team's repository (depending on what the changes are you may still have to change your seed data scripts of course) The second approach is hacky but gets around the issue of long builds:

    Create a sh (or ps1) script in ./Code/Repo2 to copy the files you need and invoke the docker commands you want, for example:

    #!/bin/bash
    rm -r ./db/schema
    mkdir ./db/schema
    
    cp  -r ../Repo1/db/schema ./db/schema
    
    docker-compose -f docker-compose.yml down
    docker container prune -f
    docker-compose -f docker-compose.yml up --build
    

    In the docker-compose file, simply set the context as Repo2 root and use the content of the ./db/schema directory in your dockerfile without worrying about the path. Bear in mind that you will run the risk of accidentally committing this directory to source control, but scripting cleanup actions should be easy enough.

提交回复
热议问题