Docker Compose: Specify Dockerfile Path – Example

The docker-compose build or docker-compose up --build commands read the docker-compose.yml file looking for all services containing the build configuration option and run a docker build command for each of them.

If the Dockerfile has been renamed or placed out of the context directory, you can specify the alternate path in the Docker Compose file.

Below i will show an example of how to specify the alternate path to the Dockerfile in the build section of the Docker Compose file.

Cool Tip: Tag an existent Docker image or build a new image with tags! Read more →

Specify Dockerfile in Docker Compose

Example of the Docker Compose file with the build configuration option that specifies the relative paths to the context directory and alternate Dockerfile:

# docker-compose.yml

version: '3.9'
services:
  web:
    build:
      context: ./web
      dockerfile: ./web/build/Dockerfile-alpine
      args:
        environment: dev
        status: stable
    ports:
        - 80:80

The args specified under the build key in the Docker Compose file can be transmitted to the Dockerfile as follows:

# Dockerfile-alpine

ARG environment
ARG status

RUN echo "Build environment: ${environment}"
RUN echo "Based status: ${status}"

Cool Tip: Label Docker images for better automation! Read more →