Writing a Dockerfile
5 min read Quiz at the end
Write a Dockerfile with FROM, WORKDIR, COPY, RUN, EXPOSE, and CMD instructions.
Writing a Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
# Build
docker build -t myapp:1.0 .
docker build -t myapp:latest -f Dockerfile.prod .
Topic Quiz · 3 questions
Test your understanding before moving on
1. Which instruction sets the working directory in a Dockerfile?
💡 WORKDIR sets the working directory for all subsequent RUN, COPY, CMD instructions.
2. What is the purpose of EXPOSE in a Dockerfile?
💡 EXPOSE is documentation only — it does not publish the port. Use -p at runtime.
3. Which instruction runs a shell command during the build?
💡 RUN executes commands in a new layer during image build (e.g. installing packages).