How to fix WORKDIR: path must be absolute in Docker
This error occurs when you use a relative path in a Dockerfile WORKDIR instruction without first establishing an absolute base path. Docker requires WORKDIR paths to start with a leading slash (/) to ensure predictable, portable builds.
Quick fix
Fixes WORKDIR: path must be absolute
# Problematic examples:
WORKDIR app # Missing leading slash
WORKDIR usr/share/nginx/html # Missing leading slash
WORKDIR ./app # Relative path with ./- Identify the problematic WORKDIR instruction
- Add the leading slash to make the path absolute
- Fix sequential WORKDIR instructions
- Verify your fix by building the image
# Problematic examples:WORKDIR app # Missing leading slashWORKDIR usr/share/nginx/html # Missing leading slashWORKDIR ./app # Relative path with ./WORKDIR: path must be absolute
What this error means
When Docker encounters "WORKDIR: path must be absolute," it means you've specified a directory path that doesn't start from the filesystem root. The WORKDIR instruction sets the working directory for subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. Docker requires this path to be absolute (starting with `/`) because relative paths depend on the base image's working directory, which can change unexpectedly between image versions. Using a relative path like `WORKDIR app` or `WORKDIR usr/share/nginx/html` is problematic because: - The base image's WORKDIR could change in future versions - Different base images have different default working directories - It makes the build less predictable and harder to debug Docker's build checks flag this as a warning (WorkdirRelativePath) to help you write more maintainable Dockerfiles.
Typical symptoms
- Docker build fails with 'WORKDIR: path must be absolute' error
- Build check warning: WorkdirRelativePath in Dockerfile
- VS Code Docker extension shows 'WORKDIR paths should be absolute' warning
- CI/CD pipeline fails during image build step
- Dockerfile linter flags WORKDIR instruction as invalid
- Build works locally but fails in different environment due to base image differences
Common causes
- Missing leading slash (/) in WORKDIR path - e.g., WORKDIR app instead of WORKDIR /app
- Typo in path where leading slash was accidentally omitted
- Copy-paste error from documentation or examples that used relative paths
- Confusion between host paths and container paths
- Using WORKDIR to set a relative subdirectory without first setting an absolute base
- Migrating from a script that used cd commands with relative paths
How to fix "WORKDIR: path must be absolute"
Find the WORKDIR line in your Dockerfile that's missing the leading slash:
# Problematic examples:
WORKDIR app # Missing leading slash
WORKDIR usr/share/nginx/html # Missing leading slash
WORKDIR ./app # Relative path with ./Check your Dockerfile for any WORKDIR instruction that doesn't start with /.
Convert the relative path to an absolute path by adding a leading slash:
# Before (incorrect):
WORKDIR app
# After (correct):
WORKDIR /app# Before (incorrect):
WORKDIR usr/share/nginx/html
# After (correct):
WORKDIR /usr/share/nginx/htmlThe leading slash ensures Docker creates or navigates to the directory starting from the container's root filesystem.
If you're using multiple WORKDIR instructions to navigate into subdirectories, ensure the first one is absolute:
# Incorrect pattern:
WORKDIR app
WORKDIR server
# Correct pattern - first WORKDIR is absolute:
WORKDIR /app
WORKDIR server # This is now relative to /app, resulting in /app/server
# Or use a single absolute path:
WORKDIR /app/serverWhile subsequent WORKDIR instructions can be relative (they build on the previous WORKDIR), it's clearer to use absolute paths throughout.
Test that your Dockerfile builds successfully:
# Build the image
docker build -t myimage .
# Verify the WORKDIR was set correctly
docker run --rm myimage pwd
# Should output: /app (or your specified path)If the build succeeds without warnings, you've fixed the issue.
If you're setting the working directory in docker-compose.yml, the same rule applies:
# Incorrect:
services:
app:
image: python:3.12-slim
working_dir: app # Missing leading slash
# Correct:
services:
app:
image: python:3.12-slim
working_dir: /app # Absolute path
volumes:
- ./:/appThe working_dir option in Compose maps to Docker's --workdir flag, which also requires an absolute path.
Enable Docker's build checks to catch WORKDIR issues before they cause problems:
# Enable all build checks
docker build --check .
# Or set via environment variable
DOCKER_BUILDKIT=1 docker build .You can also configure checks in your Dockerfile:
# syntax=docker/dockerfile:1
# check=error=WorkdirRelativePath
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY . .This converts the warning into a build error, ensuring relative WORKDIR paths are caught immediately.
Advanced notes
Why Docker requires absolute paths:
Docker containers have their own isolated filesystem. When you use WORKDIR, you're specifying a location within that container filesystem. Absolute paths ensure the directory location is unambiguous regardless of what the base image's default working directory is.
Relative WORKDIR behavior (when it works):
If you first set an absolute WORKDIR, subsequent relative WORKDIR instructions will work by appending to the current path:
WORKDIR /app # Sets /app as working directory
WORKDIR src # Now in /app/src
WORKDIR utils # Now in /app/src/utilsHowever, relying on relative paths for anything beyond the first WORKDIR can make Dockerfiles harder to understand at a glance.
Base image WORKDIR inheritance:
Your Dockerfile inherits the WORKDIR from its base image. For example:
- alpine starts with WORKDIR /
- node images may set WORKDIR to /app
- Official images can change their WORKDIR between versions
This is why starting with your own absolute WORKDIR is a best practice.
Build check configuration:
The WorkdirRelativePath check can be configured per-Dockerfile:
# Ignore the warning (not recommended)
# check=skip=WorkdirRelativePath
# Make it an error (recommended for CI)
# check=error=WorkdirRelativePathWindows containers:
For Windows containers, use Windows-style absolute paths:
# Windows container
WORKDIR C:\app
# or
WORKDIR /app # Also works in Windows containersCommon patterns:
# Node.js application
WORKDIR /usr/src/app
# Python application
WORKDIR /app
# Go application
WORKDIR /go/src/myapp
# Nginx static files
WORKDIR /usr/share/nginx/htmlFrequently asked questions
- What does "WORKDIR: path must be absolute" mean?
- When Docker encounters "WORKDIR: path must be absolute," it means you've specified a directory path that doesn't start from the filesystem root. The WORKDIR instruction sets the working directory for subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. Docker requires this path to be absolute (starting with /) because relative paths depend on the base image's working directory, which can change unexpectedly between image versions.
- How do I fix "WORKDIR: path must be absolute"?
- This error occurs when you use a relative path in a Dockerfile WORKDIR instruction without first establishing an absolute base path. Docker requires WORKDIR paths to start with a leading slash (/) to ensure predictable, portable builds.
- What causes "WORKDIR: path must be absolute"?
- Missing leading slash (/) in WORKDIR path - e.g., WORKDIR app instead of WORKDIR /app Typo in path where leading slash was accidentally omitted Copy-paste error from documentation or examples that used relative paths
- Is the "WORKDIR: path must be absolute" error serious?
- It is a low-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Docker Official Documentation
- ๐WorkdirRelativePath Build Check - Docker Documentation(Official Docs)
- ๐
- ๐
- ๐ฌ
- ๐ฌWhat is the point of WORKDIR on Dockerfile? - Stack Overflow(Stack Overflow)
- ๐
- ๐
- ๐