Docker Tutorial
Estimated reading: 5 minutes 32 views

📄 Dockerfile – Build Custom Docker Images with Ease

🧲 Introduction – Why Learn Dockerfile?

When working with Docker, a Dockerfile is your blueprint for creating custom container images. Instead of manually configuring containers, you can automate the image build process using simple, declarative instructions written in a plain text file.

Learning Dockerfile empowers developers and DevOps engineers to:

  • 🔁 Reproduce environments consistently
  • 🏗️ Automate image creation in CI/CD pipelines
  • 🚀 Optimize containers for performance and size

📋 Topics Covered

🧩 Topic📖 Description
What is DockerfileA script file used to automate the creation of Docker images
How to Create a DockerfileStep-by-step method to write and save a Dockerfile
How to Build an Image from DockerfileCommands and steps to convert a Dockerfile into an image
Dockerfile Instructions OverviewSummary of the most commonly used directives
Common Dockerfile DirectivesExpanded explanation of FROM, RUN, COPY, CMD, etc.
Dockerfile Best PracticesEfficiency tips for smaller and more secure images
Multi-stage BuildsAdvanced technique to reduce final image size

📦 What is Dockerfile?

A Dockerfile is a plain text file that contains a set of instructions used by Docker to build a custom image. Each instruction represents a layer in the resulting image.

🧱 Example:

FROM ubuntu:20.04
RUN apt update && apt install -y python3
CMD ["python3", "--version"]

This file tells Docker to:

  • Use Ubuntu as the base
  • Install Python3
  • Set the default command to print Python’s version

✍️ How to Create a Dockerfile to Build an Image

  1. 🗂️ Create a new project directory: mkdir myapp && cd myapp
  2. 📝 Create a file named Dockerfile: touch Dockerfile
  3. ✏️ Write your build instructions in it. Example: FROM node:20 COPY . /app WORKDIR /app RUN npm install CMD ["npm", "start"]

📌 The filename must be Dockerfile with no extension.


🛠️ How to Build an Image from a Dockerfile

Once your Dockerfile is ready:

  1. 🧱 Run the build command: docker build -t myapp-image .
  2. 🧪 Verify: docker images

📦 Your image myapp-image is now available locally and ready to run as a container.


🧠 Dockerfile Instructions Overview

Here’s a quick glance at the most common instructions used in Dockerfiles:

🏷️ Instruction🧾 Description
FROMSpecifies the base image
RUNExecutes commands to install packages or set configurations
COPYCopies files from host to container
ADDLike COPY, but supports URLs and archive unpacking
CMDSets the default command when container runs
ENTRYPOINTConfigures a fixed command to run
WORKDIRSets the working directory
ENVSets environment variables
EXPOSEDefines which port the container listens on
LABELAdds metadata to the image

🔍 Common Dockerfile Directives – Expanded

Here’s how some of these directives are used with examples:

✅ FROM

Specifies the base image:

FROM ubuntu:20.04

✅ RUN

Executes shell commands during image build:

RUN apt update && apt install -y curl

✅ COPY

Copies files from the host machine into the image:

COPY . /app

✅ CMD

Defines the default command to run:

CMD ["node", "index.js"]

✅ EXPOSE

Opens a port in the container:

EXPOSE 3000

📏 Dockerfile Best Practices

To create efficient and secure Dockerfiles:

  • ✅ Use slim or alpine base images to reduce size
  • ✅ Combine RUN commands to reduce layers: RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*
  • ✅ Use .dockerignore to exclude unnecessary files
  • ✅ Always pin versions (e.g., node:20, not latest)
  • ✅ Avoid hardcoding secrets or credentials
  • ✅ Use COPY over ADD unless you need archive extraction

🔄 Multi-Stage Builds

Multi-stage builds help reduce the final image size by separating build dependencies from the runtime environment.

🧪 Example:

# Stage 1 – Build
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

# Stage 2 – Final
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html

📦 This results in a minimal NGINX image that contains only your production build files.


📌 Summary – Recap & Takeaways

Dockerfiles are a core part of Docker image automation. With a few lines of code, you can define the entire runtime environment for your application.

🔍 Key Takeaways:

  • Dockerfile simplifies reproducible builds.
  • Common instructions include FROM, RUN, COPY, CMD, and EXPOSE.
  • Best practices help create smaller, secure, and efficient images.
  • Multi-stage builds are great for minimizing image bloat.

⚙️ Mastering Dockerfile will enhance your automation and DevOps skills, making your container workflows faster and cleaner.


❓ Frequently Asked Questions (FAQ)

❓ What is the difference between CMD and ENTRYPOINT?
CMD can be overridden at runtime. ENTRYPOINT defines a fixed command. You can use both together.


❓ Where should I place the Dockerfile?
✅ At the root of your application or project directory.


❓ Can I have multiple CMDs in a Dockerfile?
✅ No. Only the last CMD is used. To combine commands, use a shell script or ENTRYPOINT.


❓ How do I ignore files during Docker build?
✅ Use a .dockerignore file to exclude files like node_modules, .git, etc.


❓ How do I test a Dockerfile?
✅ Build the image and run it:

docker build -t test-image .
docker run -it test-image

❓ What happens if the build fails?
✅ Docker will stop at the failed instruction and show the error. You can use --no-cache for clean builds.


❓ Can I use ARG and ENV together?
✅ Yes! Use ARG to pass build-time variables and ENV for runtime configuration.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

DockerFile

Or Copy Link

CONTENTS
Scroll to Top