Docker execute shell command in ADD instruction's src_path

Viewed 40

I have a bunch of folders named 1/ 2/ 3/ in a directory which contains a dockerfile. I'm trying to add the latest folder 3/ to the docker container using the docker add instruction in the dockerfile.

I came up with this ls -r | grep "[0-9]" | head -1 to give the folder with highest valued name. But I'm unable to execute it in the docker add instruction.

DOCKERFILE:

FROM tensorflow/serving:2.8.0

WORKDIR /models

ADD ls -r | grep "[0-9]" | head -1 ./

ENV MODEL_NAME NumPlateDetector

ERROR:

ADD failed: file not found in build context or excluded by .dockerignore: stat ls: file does not exist

Any tips on how i can include that folder or get the ADD instruction to execute that line?

1 Answers

Original Answer:

That feature does not exist, you will need to template your Dockerfile. For example:

# contents of "Dockerfile.template"

FROM tensorflow/serving:2.8.0

WORKDIR /models

ADD ${directory_to_add}

ENV MODEL_NAME NumPlateDetector

And then in a shell script:

export directory_to_add="$(ls -r | grep "[0-9]" | head -1 ./)"

envsubst < Dockerfile.template > Dockerfile

Reference to Dockerfile ADD instruction for completeness:

https://docs.docker.com/engine/reference/builder/#add

Updated Alternative:

In regards to the comment by @david-maze, you can pass in a build arg if you prefer:

FROM tensorflow/serving:2.8.0

ARG directory_to_add

WORKDIR /models

ADD $directory_to_add

ENV MODEL_NAME NumPlateDetector

And then modify your build command:

export directory_to_add="$(ls -r | grep "[0-9]" | head -1 ./)"

docker image build --build-arg directory_to_add="$directory_to_add" ...
Related