How to Fix docker-compose "TypeError: You must specify a directory to build in path" Error?

If you are getting an error like the following with docker-compose:

Traceback (most recent call last):
  File "docker-compose", line [...], in <module>
  File "compose/cli/main.py", line [...], in main
  File "compose/cli/main.py", line [...], in perform_command
  File "compose/metrics/decorator.py", line [...], in wrapper
  File "compose/cli/main.py", line [...], in up
  File "compose/cli/main.py", line [...], in up
  File "compose/project.py", line [...], in up
  File "compose/service.py", line [...], in ensure_image_exists
  File "compose/service.py", line [...], in build
  File "compose/service.py", line [...], in build
  File "docker/api/build.py", line [...], in build
TypeError: You must specify a directory to build in path
[...] Failed to execute script docker-compose

It is likely that a custom/alternate name for the Dockerfile is specified in the docker-compose.yml build configuration, for example, like so:

# ...

services:
  example-service:
    build: ./Dockerfile.dev

# ...

This does not work because the build configuration only expects a string path to the build context (i.e. the directory in which the Dockerfile is located) excluding the Dockerfile name. To fix the issue, you need to specify the build configuration as an object instead, with:

  • The path specified under "context", and;
  • The custom/alternate name of the Dockerfile under "dockerfile".

For example:

# ...

services:
  example-service:
    build:
      context: .
      dockerfile: Dockerfile.dev

# ...

In addition, if the Dockerfile is located on an external resource or a network drive, and the problem persists for you, then simply moving/downloading it to your local environment and then referencing the file should solve your issue.


This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.