Ignoring .dockerignore

A colleague came to me and asked: "How to ignore .dockerignore



when building a docker image?"







After joint searches and checks, a simple solution was found, which consists of the following:







Assembled a test project:







test project

And now, in more detail on the files:







  1. Dockerfile







    FROM alpine
    COPY . .
    ENTRYPOINT cat data.txt
          
          





  2. .dockerignore







    data.txt
    README.md
          
          





  3. data.txt (the file that we will exclude during assembly)







    hello habr
          
          





  4. Dockerfile.dev







    FROM alpine
    COPY . .
    ENTRYPOINT cat data.txt
          
          





    As you can see, it is no different from the previously created Dockerfile, but here we are interested in the postfix in the file name







  5. Dockerfile.dev.dockerignore (ignore file for so called dev build)







    README.md
          
          







Now we have 2 identical Dockerfiles, with a difference only in the name, and each has its own .dockerignore, and for this to work, we need to set the DOCKER_BUILDKIT = 1 flag.







Well, now, you can check what happened:







ala prod-assembly



docker build -t with_ignore -f Dockerfile .

docker run --rm with_ignore
      
      





in the output we will see







cat: can't open 'data.txt': No such file or directory
      
      





test build



docker build -t without_ignore -f Dockerfile.dev .

docker run --rm without_ignore
      
      





in output







hello habr
      
      





PROFIT.

github








All Articles