debugdockerModeratepending
Debug: Docker image builds but container can't find files
Viewed 0 times
WORKDIRCOPYfile-not-foundmulti-stagedockerignore
Error Messages
Problem
Docker image builds successfully but the container fails because files are not where expected. 'No such file or directory' at runtime.
Solution
Common file location issues in Docker:
# Files copied to / instead of /app
# Fix:
WORKDIR /app
COPY . . # Now copies to /app/
# Check .dockerignore isn't excluding your files
# Debug: docker build and check with:
docker run --rm <image> ls -la /app/
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
# FORGOT to copy build output!
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json .
# Files copied as root but process runs as non-root
RUN chown -R node:node /app
USER node
# COPY doesn't follow symlinks by default
# Dereference with: COPY --link
# If Dockerfile is in a subdirectory:
docker build -f docker/Dockerfile . # Context is current dir
# NOT: cd docker && docker build .
docker run --rm -it <image> /bin/sh
find / -name 'missing-file' 2>/dev/null
- WORKDIR not set:
# Files copied to / instead of /app
# Fix:
WORKDIR /app
COPY . . # Now copies to /app/
- .dockerignore excludes needed files:
# Check .dockerignore isn't excluding your files
# Debug: docker build and check with:
docker run --rm <image> ls -la /app/
- Multi-stage build forgot to copy:
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
# FORGOT to copy build output!
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json .
- File permissions:
# Files copied as root but process runs as non-root
RUN chown -R node:node /app
USER node
- Symlinks not followed:
# COPY doesn't follow symlinks by default
# Dereference with: COPY --link
- Build context wrong:
# If Dockerfile is in a subdirectory:
docker build -f docker/Dockerfile . # Context is current dir
# NOT: cd docker && docker build .
- Debug technique:
docker run --rm -it <image> /bin/sh
find / -name 'missing-file' 2>/dev/null
Revisions (0)
No revisions yet.