2

I referred to the similar question, but following this method added all the files in the parent directory to the build context making it more than 8gb in my case. So the question is how to copy files from the parent directory without adding files to build context

The structure of the project is

Project
|
|--docker-files
|   |
|   |-Dockerfile
|
|--Folder
|   |
|   |--file/to/be/copied
|
|--other files

Using the -f flag , the command is docker build -f docker-files/Dockerfile . -t name which is executed in the Project directory

One of the solutions is to include the .dockerignore file but it very messy and the whole point of putting the docker file in a subfolder was to reduce the size of the build context.

Prajval M
  • 2,298
  • 11
  • 32
Kitwradr
  • 1,986
  • 3
  • 18
  • 32

2 Answers2

2

If you can't use .dockerignore and must use the directory tree as you've described it, one approach is to create an alternate Docker build context tree on your local system before you docker build.

Internally, the way docker build works is by creating a tar file of the context directory, sending it by HTTP over a Unix socket to the Docker daemon, and then unpacking it. This works even if $DOCKER_HOST points at a VM or a different system, but it means that Docker has declined to tell itself where the actual build context comes from. Copying the tree from the host to the Docker daemon is the slow part (even on a purely local system).

So you can do something like this:

rm -rf docker-build
mkdir docker-build
cp docker-files/Dockerfile docker-build
cp -a Folder docker-build
docker build -t ... docker-build

Nothing that you haven't explicitly cp'd in will be in the build context.

This also works reasonably well if for whatever reason you'd prefer to do parts of your build sequence outside of Docker (for example, reusing a perfectly good non-Docker build sequence and packaging the results). This is also a structure that a Makefile is good at maintaining, if you're otherwise using Make.

David Maze
  • 130,717
  • 29
  • 175
  • 215
1

file/to/be/copied just lies in Folder, so you just need to set this folder as build context, like next:

docker build -f docker-files/Dockerfile -t abc:1 Folder

Updated:

As you said in comments:

I don't want to use .dockerignore files because there are many files in folders , subfolders which need to be ignored. Anyways , thank you for clearing that – Kitwradr

If you have a lots of things to ignore, just one file to add, you could use !:

.dockerignore

*
!file/to/be/copied

It will first exclude all things in the folder, then give an exception for file/to/be/copied.

atline
  • 28,355
  • 16
  • 77
  • 113
  • Then it would add all the files in the Folder to build context right? I want to copy only the required files to the image – Kitwradr Jun 25 '19 at 05:19
  • So, the not needed files in `Folder` not in `Project`? Then, no way, `.dockerignore` is the only way afforded just to resolve this issue, but I don't know why you don't want to use it. – atline Jun 25 '19 at 05:21
  • I don't want to use .dockerignore files because there are many files in folders , subfolders which need to be ignored. Anyways , thank you for clearing that – Kitwradr Jun 25 '19 at 05:23
  • I think this is not a reason, you can use `!` to do it, see updated answer. – atline Jun 25 '19 at 05:33