In newer versions(>=1.8.0) of docker, you can do this

docker build -f Dockerfile.db .
docker build -f Dockerfile.web .

A big save.

EDIT: update versions per raksja's comment

EDIT: comment from @vsevolod: it's possible to get syntax highlighting in VS code by giving files .Dockerfile extension(instead of name) e.g. Prod.Dockerfile, Test.Dockerfile etc.

Answer from Shuo on Stack Overflow
Top answer
1 of 6
491

In newer versions(>=1.8.0) of docker, you can do this

docker build -f Dockerfile.db .
docker build -f Dockerfile.web .

A big save.

EDIT: update versions per raksja's comment

EDIT: comment from @vsevolod: it's possible to get syntax highlighting in VS code by giving files .Dockerfile extension(instead of name) e.g. Prod.Dockerfile, Test.Dockerfile etc.

2 of 6
297

Use docker-compose and multiple Dockerfile in separate directories

Don't rename your Dockerfile to Dockerfile.db or Dockerfile.web, it may not be supported by your IDE and you will lose syntax highlighting.

As Kingsley Uchnor said, you can have multiple Dockerfile, one per directory, which represent something you want to build.

I like to have a docker folder which holds each applications and their configuration. Here's an example project folder hierarchy for a web application that has a database.

docker-compose.yml
docker
├── web
│   └── Dockerfile
└── db
    └── Dockerfile

docker-compose.yml example:

version: '3'
services:
  web:
    # will build ./docker/web/Dockerfile
    build: ./docker/web
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  db:
    # will build ./docker/db/Dockerfile
    build: ./docker/db
    ports:
      - "3306:3306"
  redis:
    # will use docker hub's redis prebuilt image from here:
    # https://hub.docker.com/_/redis/
    image: "redis:alpine"

docker-compose command line usage example:

# The following command will create and start all containers in the background
# using docker-compose.yml from current directory
docker-compose up -d

# get help
docker-compose --help

In case you need files from parent folder(s) as part of the build context of your Dockerfile

You can still use the above solution and place your Dockerfile in a directory such as docker/web/Dockerfile, all you need is to set the build context in your docker-compose.yml like this:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: ./docker/web/Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code

The files hierarchy would loke like this:

config-on-root.ini
docker-compose.yml
docker
└── web
    ├── Dockerfile
    └── some-other-config.ini

The ./docker/web/Dockerfile will have access to desired files from the context like this:

FROM alpine:latest

COPY config-on-root.ini /
COPY docker/web/some-other-config.ini /

Also note that the build context will contain all the files of the specified directory. Make sure tou use a .dockerignore file to prevent loading undesired large files or directories to save time during build.

Here are some quick commands from tldr docker-compose. Make sure you refer to official documentation for more details.

🌐
Reddit
reddit.com › r/docker › docker-compose multiple dockerfiles?
r/docker on Reddit: Docker-compose multiple Dockerfiles?
March 2, 2022 -

I'm quite confused by the Docker docs.
I already have several stacks launched through Portainer, but I never used docker-compose before. I can't figure out how (if even possible) to import python scripts into the stack/docker-compose.yml

I'd like to compose some of my custom python scripts from different working Dockerfiles into one docker-compose.

I don't understand if the Dockerfile is still necessary and where (in this example) is specified the name of the Dockerfile/script.py to import in the docker-compose.yml.

Thanks for your time.

🌐
Baeldung
baeldung.com › home › docker › multiple dockerfiles in one project
Multiple Dockerfiles in One Project | Baeldung on Ops
March 19, 2024 - While the most basic usage of the docker-compose file usually means using images from a repository, we can also provide directory paths for build: version: '3' services: frontend: build: ./docker/frontend ports: - "8081:8081" backend: build: ./docker/backend ports: - "8080:8080" Now running docker-compose will build images from Dockerfiles: ... Moreover, in one docker-compose file, we can put services with different ways of creating an image.
🌐
Docker Community
forums.docker.com › general
Where to put multiple Dockerfiles in project - General - Docker Community Forums
July 21, 2019 - Hi all, I have a project that requires multiple Dockerfiles, each requiring different amounts of context. They are clogging up my top level directory right now, and I’m wondering what’s the best practice for storing my Dockerfile? I’m thinking to either have a docker/ directory with all the files, and in my docker-compose specify dockerfile and context explicitly.
🌐
Reddit
reddit.com › r/docker › multiple dockerfiles for single application
r/docker on Reddit: Multiple Dockerfiles for single application
October 23, 2024 -

Hello,

I was wondering what is the best way to go about creating a project with Dockerfiles. I currently have a test server, and a prod server, and want to develop locally.

Should I have 3 different Dockerfiles that build 3 different images based off which environment I need? Or should there be 1 Dockerfile and that Dockerfile configures itself based off of the environment?

Thanks in advance.

🌐
GitHub
github.com › michaelsauter › crane › issues › 155
dockerfile: ability to specify multiple Dockerfiles in the same (sub)directory · Issue #155 · michaelsauter/crane
February 24, 2015 - Have been talking on #docker IRC about this today. Some people need to have 'variant' images. Where the Dockerfile is nearly the same (tweaked a bit). So then it's neater to share the context (support files) and have multiple Dockerfiles in the same directory.
Author   dreamcat4
🌐
Divio
divio.com › blog › guide-using-multiple-dockerfiles
A Guide on Using Multiple Dockerfiles
December 7, 2023 - This speeds up the build process in subsequent builds. Dockerfile.prod # Use a smaller, more secure base image for production FROM node:20-slim # Set the working directory WORKDIR /app # Only copy the package.json and package-lock.json initially COPY package*.json ./ # Install production dependencies RUN npm install --only=production # Copy the rest of the application code COPY .
🌐
TechOverflow
techoverflow.net › 2019 › 06 › 26 › how-to-have-multiple-dockerfiles-in-one-directory
How to have multiple Dockerfiles in one directory | TechOverflow
June 26, 2019 - In case you don’t want to have separate directories for your Dockerfiles you can use the -f argument to docker build:
🌐
Nick Janetakis
nickjanetakis.com › blog › docker-tip-10-project-structure-with-multiple-dockerfiles-and-docker-compose
Docker Tip #10: Project Structure with Multiple Dockerfiles and Docker Compose — Nick Janetakis
June 30, 2017 - That’s going to build the Dockerfile for each of the services. If you’d rather have a separate docker-compose.yml file for each service you can run them together in 1 docker-compose command. Docker Tip #87 goes over the details. Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course.
Find elsewhere
🌐
Educative
educative.io › home › courses › working with containers: docker & docker compose › working with multiple dockerfiles
Managing Multiple Dockerfiles in Docker Compose
Learn how to configure docker-compose with multiple Dockerfiles for different services and environments in containerized applications.
🌐
Docker
docker.com › blog › dockerfiles-now-support-multiple-build-contexts
Dockerfiles now Support Multiple Build Contexts | Docker
May 3, 2022 - You can now define additional build ... them inside a Dockerfile the same way you previously did with build stages. Additional build contexts can be defined with a new --build-context [name]=[value] flag. The key component defines the name for your build context and the value can be: Local directory – ...
🌐
Docker Community
forums.docker.com › docker hub
Automated Build: Multiple Dockerfiles in one repository - Docker Hub - Docker Community Forums
February 11, 2016 - On Docker Hub, I can specify the actual name of the Dockerfile in the automated build, so I can have two Dockerfiles at the root and build two (or more) images from one repo. I tried to do the same thing on Docker Cloud…
🌐
GitHub
github.com › dotnet › docker-tools › issues › 46
Add support for multiple Dockerfiles in the same directory · Issue #46 · dotnet/docker-tools
August 23, 2017 - Currently the manifest's Dockerfile property must be set to a directory containing a Dockerfile. This property could be changed to either be a directory that contains a Dockerfile file or a full path to a Dockerfile. It is important that...
Author   MichaelSimons
🌐
TechTarget
techtarget.com › searchitoperations › tip › Use-multiple-Dockerfiles-for-complex-application-configuration
Use multiple Dockerfiles for complex application configuration | TechTarget
For example, in the Docker Voting App you'll find one Dockerfile for each of the five functional nodes. Each service has multiple Dockerfiles for the different implementation methods; the worker node has two Dockerfiles with specific instructions for the ASP.Net version and a second for the Java implementation.
🌐
CopyProgramming
copyprogramming.com › howto › multiple-dockerfiles
Docker: Multiple Dockerfiles
May 1, 2023 - Docker: Multiple Dockerfiles in project, When working on a project that requires the use of multiple dockerfiles, simply create each dockerfile in a separate directory. For instance, app/ db/. Each of the above directories will contain their dockerfile. When an application is being built, docker ...
🌐
Loof
blog.loof.fr › 2014 › 12 › multiple-dockerfiles-for-project.html
Multiple Dockerfiles for project :: new Blog( perso );
A possible workaround is for you to define a "Build from source" Dockerfile at project root, so it can ADD the source directory, and build binary in a target directory, then add to this directory another Dockerfile designed to produce the production image, just adding the binary with runtime ...
🌐
Atlassian
jira.atlassian.com › browse › BAM-20141
Specify Dockerfile location in Docker Build Image Tasks
In some larger projects I have several Dockerfiles each in their own sub-directories in order to organise them rather than putting them in the root directory.
🌐
GitHub
github.com › dependabot › dependabot-core › issues › 2063
Multiple docker files in one directory does not update correctly · Issue #2063 · dependabot/dependabot-core
December 24, 2019 - I have a directory with multiple docker files. Pre update: Dockerfile.local = node:11 Dockerfile.production = node:11-alpine Post update: Dockerfile.local = node:13-alpine Dockerfile.production = node:13-alpine Link to PR https://github....
Author   RogierdeRuijter
🌐
Nick Janetakis
nickjanetakis.com › blog › docker-tip-62-using-2-dockerfiles-in-the-same-compose-project
Docker Tip #62: Using 2 Dockerfiles in the Same Compose Project — Nick Janetakis
July 6, 2018 - In the web case it just uses our Dockerfile in the root of the directory but then in the webpack case it looks for Dockerfile.webpack instead. A fully working example can be found in the Docker best practices example repo that has a few web framework examples. A variant of this set up would be to set the context to ./assets and put a Dockerfile in there instead of using a different Dockerfile filename but personally I like it when all of my Docker related files are in the same directory.