
If you're working with Docker, mastering Dockerfiles is essential! A Dockerfile is a script that...
If you're working with Docker, mastering Dockerfiles is essential! ADockerfileis a script that defines how your Docker image is built. In this post, I'll break downessential Dockerfile instructionswith examples, comparisons, and best practices. ????
Every Dockerfilemuststart with FROM
, which specifies the base image.
Example:
FROM node:18
Best Practice:Useslimoralpineversions for smaller images:
FROM node:18-slim
✅Reduces image sizeand improves security.
Defines where the app runs inside the container.
Example:
WORKDIR /app
Why use it?
✔️ Avoids cd
commands.
✔️ Keeps the directory structure clean.
Both copy files into the container, butADDcan 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 youneed extraction.
Used to install dependencies or configure the container.
Example:
RUN apt-get update && apt-get install -y curl
✅Best Practice:Use &&
to reduce layers.
These define how thecontainer 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 CMDif users might override the command.
✅Use ENTRYPOINTfor fixed commands like Docker CLI tools.
Tells Docker which port the app will use.
Example:
EXPOSE 3000
Note:EXPOSEdoes notactually open the port! You still need -p
when running the container.
docker run -p 3000:3000 my-app
Used to defineconfiguration variables.
Example:
ENV NODE_ENV=production
Best Practice:Use .env
files for secrets instead of hardcoding values.
Ensures data isnot lostwhen the container stops.
Example:
VOLUME /data
✅Useful for:Databases, logs, and file storage.
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 ARGfor build-time secrets.
✅Use ENVfor runtime configs.
FROM node:18-slim
WORKDIR /app
COPY package.json .
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
✅Best Practices Applied:
RUN
.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 |
Alwaysuse multi-stage buildsandminimize image sizefor better performance!
Did you find this guide helpful? Drop a comment below! ????
Docker #DevOps #Cloud #Containers #CICD #DevopsInsiders #devins