Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to build Dockerfile with custom names

I am trying to automate the building of Docker images. Let’s say in a directory, there are several Dockerfile files. Some of them are named as Dockerfile.test or Dockerfile_node as there are multiple files in a single directory and they can’t all be named Dockerfile.

I have a simple script which locates all those files and needs to call docker build.

This is the command I use for locating all the Dockerfile.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list=$(find . -name "Dockerfile*")

And I get the following list:

./Dockerfile1
./testing/Dockerfile
./testing/Dockerfile_kubernetes

In order to get the context, I need to find the directories that contain the Dockerfile files.

files=$(find . -name "Dockerfile*" -exec dirname {} \;)

For each Dockerfile, I am calling the docker build. Something like this…

for x in $files; do docker build $x; done;

I can’t perform docker build as I get the following error.

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/ubuntu/repo/Dockerfile: no such file or directory

Running the docker build command will only build an image defined in ./testing/Dockerfile.

I know it’s bad practice to have multiple Dockerfile files in a single directory, and to name them like this, but I am not the one making these decisions. I just need to make it work.

Is there a way to build these Dockerfiles?

>Solution :

You need to pass the docker file name to build command.

for x in $files; do docker build -f $x .; done;

By default the docker build command will look for a Dockerfile at the
root of the build context. The -f, –file, option lets you specify the
path to an alternative file to use instead

https://docs.docker.com/engine/reference/commandline/build/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading