Skip to content
Latchkey

Docker "WORKDIR requires absolute path" - Fix Relative WORKDIR in CI

A WORKDIR resolved to a non-absolute path. BuildKit either rejects it outright or chains relative WORKDIRs together in a way you did not intend, so later COPY/RUN steps land in the wrong directory.

What this error means

The build fails with the working directory ... must be an absolute path, or a later step fails because files ended up under a stacked relative path (/app/app/...). The cause is a WORKDIR that is relative or expands from an empty variable.

docker build output
ERROR: failed to solve: the working directory "app" is not an absolute path
# or, relative WORKDIRs stack:
# WORKDIR /app   then   WORKDIR src   ->  /app/src (often unintended elsewhere)

Common causes

WORKDIR is a relative path

A bare WORKDIR app is relative to the previous WORKDIR. Some BuildKit frontends/strict modes reject a non-absolute path, and even when accepted, relative WORKDIRs stack in surprising ways.

WORKDIR built from an empty build arg

A WORKDIR ${APP_DIR} where APP_DIR is unset/empty collapses to an empty or relative path, producing an invalid working directory.

How to fix it

Use an absolute WORKDIR

Always give WORKDIR a leading slash so it is unambiguous.

Dockerfile
WORKDIR /app
COPY . .
RUN npm ci && npm run build

Default and validate any WORKDIR build arg

Give the arg a default and ensure it is absolute.

Dockerfile
ARG APP_DIR=/app
WORKDIR ${APP_DIR}

How to prevent it

  • Always write WORKDIR as an absolute path.
  • Default and validate any variable used to build a WORKDIR.
  • Avoid relying on relative-WORKDIR stacking for clarity.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →