Most of the time we use the conventional file name, Dockerfile to build Docker images. But there can be situations where we need to build multiple image variations for development, testing, production, architecture and many more. Of course, as you might guess we can create multiple Dockefiles. The problem is, when we build a Docker image using the below-mentioned docker build command, by default it does only recognize a Docker file which has the file name as Dockerfile.

docker build -t <tag-name> .

How To Use Custom Dockerfile Names

The docker build command accepts custom Dockerfile names. You can mention your custom Dockerfile name using the -f flag of the docker build command like below.

docker build -t <tag-name> . -f <file-name>

Build separate Docker images for every Dockerfile according to your requirements.

docker build -t dev-image . -f dev.Dockerfile
docker build -t test-image . -f test.Dockerfile
docker build -t prod-image . -f prod.Dockerfile

Custom Dockerfile Names In Subdirectories

Placing a lot of Dockerfiles in the root directory of your project can make your project quite look unorganized and messy, so we can even place them all in a subdirectory (Ex: “docker”). Mention the paths of custom Dockerfiles using the -f flag like below. You can add the ./ part to the path of -f flag to indicate the current directory, so you or your team members will not have to mention the full paths to Dockerfiles.

docker build -t amd64-image . -f ./docker/amd64.Dockerfile
docker build -t i386-image . -f ./docker/i386.Dockerfile