KhueApps
Home/DevOps/Fix 'Cannot locate specified Dockerfile' and 'open Dockerfile' Errors

Fix 'Cannot locate specified Dockerfile' and 'open Dockerfile' Errors

Last updated: October 07, 2025

Overview

These errors mean the build process can’t find your Dockerfile:

  • Docker CLI: "open Dockerfile: no such file or directory"
  • Docker Compose: "Cannot locate specified Dockerfile: Dockerfile"

They occur due to wrong working directories, incorrect paths, case mismatches, misconfigured Compose build settings, or an unexpected build context.

Quickstart: The fast fixes

  1. Verify the file name is exactly "Dockerfile" (case-sensitive on Linux CI).
  2. Run the build from the directory that contains the Dockerfile, or pass -f:
    • docker build -t app:latest .
    • docker build -f docker/Dockerfile -t app:latest .
  3. For Compose, set both context and dockerfile correctly:
    • docker compose build
  4. Ensure the Dockerfile is inside the build context directory.
  5. Check .dockerignore isn’t excluding the Dockerfile directory or renaming paths.
  6. In CI, confirm the job’s working directory and checkout paths.

Minimal working example

Directory layout:

  • project/
    • Dockerfile
    • hello.txt

Dockerfile:

FROM alpine:3.20
COPY hello.txt /hello.txt
CMD ["cat", "/hello.txt"]

Build and run:

cd project
# Works when run in the directory with the Dockerfile
docker build -t demo:latest .
docker run --rm demo:latest

Using a subdirectory:

# If Dockerfile is in docker/Dockerfile
docker build -f docker/Dockerfile -t demo:latest .

Compose example:

a version: "3.9"
services:
  web:
    build:
      context: .
      dockerfile: docker/Dockerfile
    image: demo:latest
docker compose build

Common causes and precise fixes

  1. Wrong working directory
  • Symptom: CLI error "open Dockerfile: no such file or directory".
  • Fix: cd into the folder with the Dockerfile, or pass -f with a valid path.
    • docker build -t app .
    • docker build -f docker/Dockerfile -t app .
  1. Misunderstanding -f vs context
  • The last argument to docker build is the build context (often .).
  • -f path is resolved relative to your current shell directory, not the context.
  • Fix: Provide a valid -f relative to the current directory, and ensure the Dockerfile is inside the context.
    • docker build -f ./docker/Dockerfile -t app .
  1. Compose path rules
  • In docker compose, build.dockerfile is resolved relative to build.context.
  • Fix in compose.yaml:
    • build: context: . dockerfile: docker/Dockerfile
  • Run from the directory containing the compose file (or pass -f path/to/compose.yaml).
  1. Case sensitivity and file naming
  • On Linux runners, "Dockerfile" ≠ "dockerfile".
  • Windows/macOS dev machines might hide extensions; "Dockerfile.txt" won’t be found.
  • Fix: Rename to exact "Dockerfile" or configure -f path to the actual name.
  1. Dockerfile outside context
  • The build context is what gets sent to the daemon. The Dockerfile must be within this context (Compose requires this; CLI can read -f outside but referenced files for COPY must be inside context).
  • Fix: Set context to the project root that includes sources you COPY and keep Dockerfile within it.
  1. .dockerignore excluding needed paths
  • Overly broad patterns (e.g., "**" or excluding the folder that holds the Dockerfile when using a non-default name/location) can break builds.
  • Fix: Ensure .dockerignore does not exclude the Dockerfile directory. Keep patterns specific (e.g., node_modules/, .git/, dist/).
  1. CI/CD working directory
  • Pipelines often default to repo root or a different subdirectory.
  • Fix: Explicitly set the working directory before building, or pass correct -f and context.
# GitHub Actions example step
- name: Build
  run: |
    cd services/api
    docker build -t myorg/api:ci .
  1. Compose file location vs context
  • If you run docker compose from a different directory, Compose resolves relative paths differently.
  • Fix: Run from the compose file’s directory or specify -f path/to/compose.yaml.

Symptom-to-fix cheat sheet

  • "open Dockerfile: no such file or directory"
    • Use correct working directory or add -f path; ensure Dockerfile exists.
  • "Cannot locate specified Dockerfile: Dockerfile"
    • In compose.yaml, align build.context and build.dockerfile; ensure file is inside context.
  • Works locally, fails in CI
    • Check case sensitivity, checkout path, and explicit cd.

Performance notes

  • A correct context speeds builds: use the smallest context that still includes sources you COPY.
  • Use .dockerignore to exclude heavy directories (.git/, node_modules/, build artifacts, test data).
  • Passing -f does not reduce context size; only the final context argument controls what’s sent.
  • Multi-stage builds reduce image size and cache layers effectively.
  • BuildKit (default) improves caching; keep Dockerfile and COPY order stable for cache hits.

Step-by-step: Diagnose and fix

  1. Confirm location
    • ls -la to ensure a file named Dockerfile exists where you think it does.
  2. Re-run with explicit paths (CLI)
    • docker build -f path/to/Dockerfile -t app path/to/context
  3. Validate Compose config
    • build.context points to project root; build.dockerfile relative to that context.
  4. Check .dockerignore
    • Ensure it doesn’t exclude the Dockerfile’s folder when using custom locations.
  5. Normalize file name
    • Exact "Dockerfile" casing; remove hidden extensions.
  6. Fix CI working dir
    • cd into the correct subdirectory before docker build or set paths in your script.
  7. Rebuild
    • docker build or docker compose build; verify no path errors remain.

Tiny FAQ

  • Can the Dockerfile be outside the build context?

    • CLI can read it with -f, but files referenced by COPY must be inside context. Compose requires dockerfile to be within context.
  • Do I need to name it exactly "Dockerfile"?

    • No; use -f for custom names. In Compose, set build.dockerfile accordingly.
  • Why does it work on my Mac but fail in CI?

    • Likely case sensitivity or different working directory. Linux CI treats file names case-sensitively.
  • Does .dockerignore hide the Dockerfile?

    • It can hide directories containing the Dockerfile when you use custom locations. Keep patterns precise.

Series: Docker

DevOps