Home > database >  How to build Dockerfile with custom names
How to build Dockerfile with custom names

Time:04-04

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.

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?

CodePudding user response:

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/

  • Related