Main goal
A good image should work well without carrying extra files, tools, or unnecessary weight.
This page focuses on making images smaller, faster to build, easier to transfer, and cleaner for real project use.
A good image should work well without carrying extra files, tools, or unnecessary weight.
Smaller images usually mean faster builds, faster downloads, quicker deployments, and easier maintenance.
Optimization should improve clarity and efficiency, not make the Dockerfile confusing too early.
FROM node:18
# smaller option
FROM node:18-alpine
Smaller base images often reduce image size and download time, as long as they still fit your app needs.
node_modules
.git
.env
dist
coverage
npm-debug.log
This prevents unnecessary files from being sent into the Docker build context.
FROM node:18-alpine
WORKDIR /app
COPY package.json /app
RUN npm install
COPY server.js /app
EXPOSE 3000
CMD ["npm", "start"]
Copy dependency files first so Docker can reuse cached install layers when app code changes later.
If you copy the whole project before dependency installation, even a small file change may force Docker to reinstall everything again.
FROM node:18 AS builder
# build steps
FROM node:18-alpine
# copy only final output
One stage builds the app, the final stage keeps only what is needed to run it.
Next page: Lesson 12 focuses on Dockerfile best practices so you can write cleaner and more professional Docker build files.