Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Write a Dockerfile that runs as non-root but still needs to write to a directory.

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM node:22-alpine
WORKDIR /app
COPY --chown=node:node . .
RUN mkdir -p /app/uploads && chown node:node /app/uploads
USER node
VOLUME ["/app/uploads"]
CMD ["node", "server.js"]

Key trick → Create and chown the writable directory before USER, because after it you no longer have permission. In Kubernetes, securityContext.fsGroup handles volume ownership.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks