The COPY
instruction is tightly linked to the context you will build this image in.
This is actually pointed in the documentation:
The docker build
command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH
or URL
. The build process can refer to any of the files in the context. For example, your build can use a COPY
instruction to reference a file in the context.
Source: https://docs.docker.com/engine/reference/commandline/build/#extended-description
This said, although one will usually do:
docker build .
You have to understand that, this last dot .
, that you surely have seen Docker complain about, when you've forgotten providing it, in some occasion, is the actual context in which this build will happen.
You are not forced to have .
— so the current directory — as your context though.
So, what you will have to aim at is to provide the right context, which will be a folder that would be a common ancestor between /src/Shared Settings
and the folder your Dockerfile resides.
You could end up with a big downside, with the folder Shared Settings
being only in src
, because the only common ancestor you will find might force you to use /
as your context, making you an humongous context and so a really heavy build.
To come around this, you can use a .dockerignore file, though, to ignore all unrelated folders but the one you are want to make use of; namely /src/Shared Settings
and the folder containing your Dockerfile.
Also, as you change your context, mind that you will have to adapt the line
COPY . .
That will now read:
COPY name_of_the_folder_containing_the_Dockerfile .
Here is an example that you will have to adapt based on your actual folders hierarchy.
Here the folders hierarchy:
.
├── .dockerignore
└── src
├── docker
│ └── Dockerfile
└── shared_settings
└── foo.conf
In the Dockerfile:
FROM alpine
COPY src/shared_settings /etc/opt/app
WORKDIR /opt/app
COPY src/docker .
## The new equivalent to your previous `COPY . .`
CMD ["ls","-l"]
Here is how the file .dockerignore does exclude the folder unrelated from the context:
**
!src/docker
!src/shared_settings
And, now I can either go in the folder src/docker and build with:
docker build -f Dockerfile ../..
Or go in the folder / and build with
docker build -f src/docker/Dockerfile .
The only important thing is that both the folder containing the Dockerfile and the folder shared_settings should be part of my context.