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)