Reputation: 317
I have to create a Dockerfile to copy certain zip files. Assume the files are
/opt/source
A.zip
B.zip
C.zip
D.zip
Dockerfile
When building Dockerfile, I will be passing the file names as an argument in arraylist format, as below:
docker build -t mch --build-arg arg1="A.zip B.zip C.zip D.zip" --no-cache .
The crux is file names can vary each time when building the Dockerfile; for example, only A, B and C file will only be available or sometime more file will be available or at times there could be zip files that shouldn't be copied at all. Only the ones mentioned in the argument should be picked.
How could I complete file to iterate through arg1 arraylist to copy the files from local to container while building? Different approach on this are welcome.
My Dockerfile contains:
# The official Red Hat registry and the base image
FROM registry.access.redhat.com/ubi8/ubi:8.2
ARG arg1
RUN yum install -y unzip && \
yum install -y procps && \
mkdir -p /opt/dict
WORKDIR /opt/dict
COPY $arg1 .
Upvotes: 1
Views: 1278
Reputation: 159081
I can think of three reasonable approaches to do this, depending on what exactly you're trying to copy in.
Separate Dockerfile per file set. Say these images are substantially different in some predictable way; one image has application-a.jar
and static-files.zip
, and another has application-b.jar
, static-files.zip
, and public-keys.zip
. You can create two separate Dockerfiles
# a.dockerfile
COPY application-a.jar static-files.zip .
# b.dockerfile
COPY application-b.jar static-files.zip public-keys.zip .
and then build the separate images
docker build -t me/application-a -f a.dockerfile .
docker build -t me/application-b -f b.dockerfile .
Manually construct the build context. On the host system, you can create a directory tree that contains only the specific files you need:
rm -rf build-context
mkdir build-context
cp Dockerfile application-a.jar static-files.zip build-context
docker build -t me/application-a build-context
Nothing that's not in this build-context
directory will be accessible to the Dockerfile, so you can directly put
COPY . . # copies everything in the build context
(I used this approach with a host build system to simulate what multi-stage builds would eventually do, before there were multi-stage builds. If the files you're omitting are large, this can result in a faster build as well.)
Edit the Dockerfile. You can create a template Dockerfile that looks like
# Dockerfile.tmpl
COPY %FILES% .
and then use a tool like sed(1) to fill in the template variables
sed 's/%FILES%/application-a.jar static-files.zip/' Dockerfile.tmpl > Dockerfile
You might be able to use envsubst(1), with a little easier syntax, but it could conflict with the native Dockerfile variable substitution or variable references in RUN
statements. You need to be careful with quoting and syntax if there are punctuation conflicts.
Upvotes: 1