How to pass file as argument for a dockerfile
Solution 1:
I've managed to solve this after two minutes of reading in docker's doc.
The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg = flag. If a user specifies a build argument that was not defined in the Dockerfile, the build outputs an error.
So I've used this to build my image
docker build -t myapp/v1 --build-arg package=package_myapp_v1.tar.gz .
Then to get the package inside Dockerfile
use ARG
instruction:
ARG package
ADD $package ./
The content of $package
variable is package_myapp_v1.tar.gz
which is passed through the tag package
.
Solution 2:
It seems like it is the context that should be changing between the builds, not the Dockerfile, right? Either way, one way to achieve this is to use Docker's ability to receive the context as a tar (optionally gz) file, with the Dockerfile at its root:
tar --create \
/some/path/to/Dockerfile \
/other/path/to/package-X.Y.Z.tgz \
--transform 's,.*/,,' \
--transform 's/package.*tgz/package.tgz/' |
docker build -t myapp/myapp:v1 -
Note:
The first
transform
flattens the directory structure, so all files appear in the root of thetar
file, where Docker expects them.The second
transform
removes the version number from the package file, so that you don't have to edit theADD package.tgz .
line in your Dockerfile.