a Dockerfile Explained
What a Dockerfile does, with an example.
Published November 9, 2026
A Dockerfile is a text file with instructions to build an image: base image, files, dependencies, environment and the start command.
How it works
Common instructions: FROM, WORKDIR, COPY, RUN, ENV, EXPOSE, USER, CMD and ENTRYPOINT. Each creates a layer.
Order matters for build caching: copy dependency files first, install, then copy the source.
Try it
cat Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]
Practical examples
Node app
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]PHP app
FROM php:8.3-fpm
RUN docker-php-ext-install pdo_mysql
COPY . /var/www/htmlBuild it
docker build -t myapp .Check layers
docker history myappCommon mistakes
- Running as root.
- COPY . . before installing dependencies.
- Leaving build tools and caches in the final image.
Key takeaways
- A Dockerfile is a text file with instructions to build an image: base image, files, dependencies, environment and the start command.
- Run docker <command> --help for all options.
- Clean up unused resources regularly.
FAQ
CMD vs RUN?
RUN executes at build time; CMD runs when the container starts.
Where can I see all options?
Run the command with --help.