Setting Up Dockerfile
Dockerfile instructions

Cloud & DevOps Engineer | Ex-Virtusan | AWS | Terraform | Jenkins | Docker | Kubernetes |
If you're starting with Docker, understanding Dockerfile instructions is very important.
A Dockerfile is simply a blueprint used to build Docker images.
Let’s break down the most commonly used Dockerfile components 👇
FROM - Specify the base image.
RUN - Execute a command in the image during build.
CMD ["executable","param1","param2"] - Specify the default command to run in a container.
ENTRYPOINT ["executable", "param1", "param2"] - Set the entry point for a container.
WORKDIR /path - Set the working directory for instructions.
COPY - Copy files or directories from host to image.
ADD - Copy files or directories from host to image with URL support and automatic unpacking of compressed files.
ENV - Set environment variables in the image.
EXPOSE - Inform Docker that the container listens on the specified network ports at runtime.
VOLUME ["/data"] - Create a mount point with the specified path and mark it as holding externally mounted volumes from native host or other containers.
1️⃣ FROM – Base Image
Specifies the base image for your Docker image.
Every Dockerfile must start with FROM (except multi-stage builds where it may appear multiple times).
FROM ubuntu:22.04
📌 Example:
FROM node:18
2️⃣ RUN – Execute Commands During Build
Used to execute commands while building the image.
RUN apt update && apt install -y nginx
👉 This runs only during the image build process.
3️⃣ CMD – Default Command
Specifies the default command to run when a container starts.
CMD ["executable", "param1", "param2"]
📌 Example:
CMD ["node", "app.js"]
⚡ Only one CMD is allowed. If multiple are provided, the last one will be used.
4️⃣ ENTRYPOINT – Main Container Process
Sets the main command that always runs when the container starts.
ENTRYPOINT ["executable", "param1", "param2"]
📌 Example:
ENTRYPOINT ["python3"]
🆚 Difference:
CMDcan be overridden.ENTRYPOINTis harder to override.
5️⃣ WORKDIR – Set Working Directory
Sets the working directory for all following instructions.
WORKDIR /app
📌 Automatically creates the directory if it doesn't exist.
6️⃣ COPY – Copy Files
Copies files from your local system into the Docker image.
COPY source destination
📌 Example:
COPY . /app
7️⃣ ADD – Copy with Extra Features
Similar to COPY but:
Supports URLs
Automatically extracts compressed files
ADD file.tar.gz /app
⚠️ Best Practice: Prefer COPY unless you specifically need ADD features.
8️⃣ ENV – Environment Variables
Sets environment variables inside the image.
ENV APP_ENV=production
📌 Used for configuration settings.
9️⃣ EXPOSE – Declare Container Port
Informs Docker which port the container listens on.
EXPOSE 8080
⚠️ Note: It does NOT publish the port.
You still need -p option while running container.
🔟 VOLUME – Persistent Storage
Creates a mount point for external storage.
VOLUME ["/data"]
📌 Used for:
Databases
Logs
Persistent files
🔥 Simple Example Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]


