0

I want to include files outside of Docker's build context using context change and so that .dockerignore works. But to work correctly, I need to move .dockerignore to the updated build context directory. How to avoid it?

Works:

$ ls -a
.  ..  Dockerfile  .dockerignore  sourses  ulala
$ sudo docker build -t docker_test -f Dockerfile .
$ sudo docker run -it docker_test bash
# ls
Dockerfile  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  sourses  srv  sys  tmp  usr  var

Don't work:

$ ls -a
.  ..  Dockerfile  .dockerignore  sourses  ulala
$ mkdir dock_dir
$ cd dock_dir/
$ cp ../Dockerfile ./Dockerfile
$ rm ../Dockerfile
$ cp ../.dockerignore ./.dockerignore
$ rm ../.dockerignore
$ sudo docker build -t docker_test -f Dockerfile ..
$ sudo docker run -it docker_test bash
# ls
bin  boot  dev  dock_dir  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  sourses  srv  sys  tmp  ulala  usr  var

Works, but I don't want it

$ ls -a
.  ..  Dockerfile  .dockerignore  sourses  ulala
$ mkdir dock_dir
$ cd dock_dir/
$ cp ../Dockerfile ./Dockerfile
$ rm ../Dockerfile
$ sudo docker build -t docker_test -f Dockerfile ..
$ sudo docker run -it docker_test bash
# ls
Dockerfile  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  sourses  srv  sys  tmp  usr  var

Dockerfile:

FROM debian:latest
ADD  . .

.dockerignore:

**/ulala/**/*
**/ulala
  • As of Docker 19.03, with the BuildKit backend, you can create a `dock_dir/Dockerfile.dockerignore` file; see [How to specify different .dockerignore files for different builds in the same project?](https://stackoverflow.com/questions/40904409/how-to-specify-different-dockerignore-files-for-different-builds-in-the-same-pr). A plain `.dockerignore` file _must_ be in the context root directory and not anywhere else; [Docker-compose and .dockerignore](https://stackoverflow.com/questions/53945934/docker-compose-and-dockerignore). – David Maze May 17 '22 at 10:07
  • (Personally, I'd recommend putting the `Dockerfile` and `.dockerignore` in the project root directory, since that's where the tools expect them to be.) – David Maze May 17 '22 at 10:08

0 Answers0