If you're working with Docker, mastering Dockerfiles is essential! A Dockerfile is a script that defines how your Docker image is built. In this post, I'll break down essential Dockerfile instructions with examples, comparisons, and best practices. ๐ฅ
1๏ธโฃ FROM โ Define Base Image
Every Dockerfile must start with FROM, which specifies the base image.
๐น Example:
FROM node:18
๐ Best Practice: Use slim or alpine versions for smaller images:
FROM node:18-slim
โ Reduces image size and improves security.
2๏ธโฃ WORKDIR โ Set Working Directory
Defines where the app runs inside the container.
๐น Example:
WORKDIR /app
๐ Why use it?
โ๏ธ Avoids cd commands.
โ๏ธ Keeps the directory structure clean.
3๏ธโฃ COPY vs ADD โ Copying Files
Both copy files into the container, but ADD can also extract archives.
| Instruction | Function | Extracts .tar.gz? |
Supports URLs? |
|---|---|---|---|
| COPY | Copies files/folders | โ No | โ No |
| ADD | Copies + extracts files | โ Yes | โ Yes |
๐น Example:
COPY package.json .
ADD my-archive.tar.gz /data/
โ
Best Practice: Use COPY unless you need extraction.
4๏ธโฃ RUN โ Execute Commands During Build
Used to install dependencies or configure the container.
๐น Example:
RUN apt-get update && apt-get install -y curl
โ
Best Practice: Use && to reduce layers.
5๏ธโฃ CMD vs ENTRYPOINT โ Whatโs the Difference?
These define how the container starts, but they work differently.
| Instruction | Purpose | Can Be Overridden? |
|---|---|---|
| CMD | Default command | โ Yes |
| ENTRYPOINT | Fixed command | โ No |
๐น CMD Example:
CMD ["node", "app.js"]
๐น ENTRYPOINT Example:
ENTRYPOINT ["java", "-jar", "app.jar"]
โ
Use CMD if users might override the command.
โ
Use ENTRYPOINT for fixed commands like Docker CLI tools.
6๏ธโฃ EXPOSE โ Define Port
Tells Docker which port the app will use.
๐น Example:
EXPOSE 3000
๐ Note: EXPOSE does not actually open the port! You still need -p when running the container.
docker run -p 3000:3000 my-app
7๏ธโฃ ENV โ Set Environment Variables
Used to define configuration variables.
๐น Example:
ENV NODE_ENV=production
๐ Best Practice: Use .env files for secrets instead of hardcoding values.
8๏ธโฃ VOLUME โ Persistent Storage
Ensures data is not lost when the container stops.
๐น Example:
VOLUME /data
โ Useful for: Databases, logs, and file storage.
9๏ธโฃ ARG vs ENV โ Build vs Runtime Variables
| Instruction | Purpose | Available at Runtime? |
|---|---|---|
| ARG | Temporary build-time variable | โ No |
| ENV | Runtime configuration | โ Yes |
๐น Example:
ARG BUILD_VERSION=1.0
ENV APP_ENV=production
โ
Use ARG for build-time secrets.
โ
Use ENV for runtime configs.
๐ฅ Optimized Dockerfile Example
FROM node:18-slim
WORKDIR /app
COPY package.json .
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
โ Best Practices Applied:
- Uses a slim base image.
- Sets a working directory.
-
Minimizes layers in
RUN. - Avoids unnecessary files.
- Uses CMD for flexibility.
๐ Summary
| Instruction | Purpose |
|---|---|
| FROM | Defines the base image |
| WORKDIR | Sets working directory |
| COPY vs ADD | Copies files (ADD also extracts) |
| RUN | Executes build commands |
| CMD vs ENTRYPOINT | Defines how the container runs |
| EXPOSE | Declares port usage |
| ENV | Sets environment variables |
| ARG | Temporary build-time variables |
| VOLUME | Persistent storage |
๐ก Final Tip
Always use multi-stage builds and minimize image size for better performance!
Did you find this guide helpful? Drop a comment below! ๐
Top comments (0)