This behavior is given by the context directory that the docker
or podman
uses to present the files to the build process.
A nice trick here is by changing the context dir during the building instruction to the full path of the directory, that you want to expose to the daemon.
e.g:
docker build -t imageName:tag -f /path/to/the/Dockerfile /mysrc/path
using /mysrc/path
instead of .
(current directory), you'll be using that directory as a context, so any files under it can be seen by the build process.
This example you'll be exposing the entire /mysrc/path
tree to the docker daemon.
When using this with docker
the user ID who triggered the build must have recursively read permissions to any single directory or file from the context dir.
This can be useful in cases where you have the /home/user/myCoolProject/Dockerfile
but want to bring to this container build context, files that aren't in the same directory.
Here is an example of building using context dir, but this time using podman
instead of docker
.
Lets take as example, having inside your Dockerfile
a COPY
or ADD
instruction which is copying files from a directory outside of your project, like:
FROM myImage:tag
...
...
COPY /opt/externalFile ./
ADD /home/user/AnotherProject/anotherExternalFile ./
...
In order to build this, with a container file located in the /home/user/myCoolProject/Dockerfile
, just do something like:
cd /home/user/myCoolProject
podman build -t imageName:tag -f Dockefile /
Some known use cases to change the context dir, is when using a container as a toolchain for building your souce code.
e.g:
podman build --platform linux/s390x -t myimage:mytag -f ./Dockerfile /tmp/mysrc
or it can be a path relative, like:
podman build --platform linux/s390x -t myimage:mytag -f ./Dockerfile ../../
Another example using this time a global path:
FROM myImage:tag
...
...
COPY externalFile ./
ADD AnotherProject ./
...
Notice that now the full global path for the COPY
and ADD
is omitted in the Dockerfile command layers.
In this case the contex dir
must be relative to where the files are, if both externalFile
and AnotherProject
are in /opt
directory then the context dir
for building it must be:
podman build -t imageName:tag -f ./Dockerfile /opt
Note when using COPY
or ADD
with context dir in docker
:
The docker
daemon will try to "stream" all the files visible on the context dir tree to the daemon, which can slowdown the build. And requires the user to have recursively permission from the context dir.
This behavior can be more costly specially when using the build through the API. However,with podman
the build happens instantaneously, without needing recursively permissions, that's because podman
does not enumerate the entire context dir, and doesn't use a client/server
architecture as well.
The build for such cases can be way more interesting to use podman
instead of docker
, when you face such issues using a different context dir.
Some references:
FROM
to continue from there. I would not change the project structure to accommodate Docker (or any build tools).docker buildx build --build-context othersource= ../something/something .
see answer below