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.
Related videos on Youtube
storm
Updated on September 18, 2022Comments
-
storm over 1 year
Actually I'm not sure this can be done but I know there is always a hack , so I am enthusiastic about what you experts will say about this.
l want to pass a package as an argument when building my docker image so that I can deploy separate app versions with the same dockerfile.
This is an example for what I want to achieve :
The ability to pass package as an argument when building the image ( I know the syntax is wrong , it's just an example )docker build -t myapp/myapp:v1 . package_myapp_v1.tar
Then use
ADD
in theDockerfile
to extract it into the container.ADD passed_package ./
So that if I want to build an image for my app's v2 I can just specify the new package in
docker build
command without changing theDockerfile
.Anyone knows what's the trick to get that working?
Thanks :)