DEV Community

Cover image for Mastering Dockerfile: Key Instructions Explained in Simple Terms
Arun Kumar
Arun Kumar

Posted on

Mastering Dockerfile: Key Instructions Explained in Simple Terms

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
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Best Practice: Use slim or alpine versions for smaller images:

FROM node:18-slim
Enter fullscreen mode Exit fullscreen mode

βœ… Reduces image size and improves security.


2️⃣ WORKDIR – Set Working Directory

Defines where the app runs inside the container.

πŸ”Ή Example:

WORKDIR /app
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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/
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

βœ… 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"]
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή ENTRYPOINT Example:

ENTRYPOINT ["java", "-jar", "app.jar"]
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Note: EXPOSE does not actually open the port! You still need -p when running the container.

docker run -p 3000:3000 my-app
Enter fullscreen mode Exit fullscreen mode

7️⃣ ENV – Set Environment Variables

Used to define configuration variables.

πŸ”Ή Example:

ENV NODE_ENV=production
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

βœ… 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"]
Enter fullscreen mode Exit fullscreen mode

βœ… 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! πŸš€

Docker #DevOps #Cloud #Containers #CICD #DevopsInsiders #devins

Top comments (0)