DEV Community

Devtonight
Devtonight

Posted on • Updated on • Originally published at devtonight.com

How To Build Docker Images With Custom Dockerfile Names

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> .
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

docker build -t dev-image . -f dev.Dockerfile
Enter fullscreen mode Exit fullscreen mode
docker build -t test-image . -f test.Dockerfile
Enter fullscreen mode Exit fullscreen mode
docker build -t prod-image . -f prod.Dockerfile
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
docker build -t i386-image . -f ./docker/i386.Dockerfile
Enter fullscreen mode Exit fullscreen mode

Feel free to visit devtonight.com for more related content.

Top comments (0)