Dockerfile Explained: Complete Guide for Beginners

 



Dockerfile Explained: Complete Guide for Beginners

Introduction

A Dockerfile is one of the most important components of Docker. It is a text file that contains a set of instructions used to automatically build Docker Images.

Instead of manually configuring containers every time, Dockerfiles allow developers to define application environments as code. This makes deployments consistent, repeatable, and easy to manage.

In modern DevOps practices, Dockerfiles play a critical role in CI/CD pipelines, containerized applications, and cloud-native deployments.

In this guide, we will learn Dockerfile architecture, instructions, best practices, and real-world examples.


What is a Dockerfile?

A Dockerfile is a text file containing instructions that Docker uses to build an image.

Docker reads the Dockerfile line by line and creates an image layer for each instruction.

Example:

FROM nginx
COPY . /usr/share/nginx/html

This Dockerfile creates an Nginx image and copies website files into it.


Why Do We Need a Dockerfile?

Without Dockerfile:

  • Manual configuration

  • Inconsistent environments

  • Difficult deployments

  • More human errors

With Dockerfile:

  • Automated image creation

  • Consistent deployments

  • Easy version control

  • Faster CI/CD pipelines


Dockerfile Workflow

Application Code
        ↓
    Dockerfile
        ↓
 docker build
        ↓
 Docker Image
        ↓
 docker run
        ↓
 Docker Container

Basic Dockerfile Structure

FROM ubuntu:22.04

WORKDIR /app

COPY . .

RUN apt-get update

CMD ["bash"]

Docker executes these instructions from top to bottom.


Important Dockerfile Instructions

1. FROM

The FROM instruction specifies the base image.

Example:

FROM ubuntu:22.04

or

FROM nginx:latest

Every Dockerfile starts with a FROM instruction.


2. WORKDIR

Sets the working directory inside the container.

Example:

WORKDIR /app

All future commands execute from this directory.


3. COPY

Copies files from the local system to the container.

Example:

COPY . .

or

COPY app.py /app/

4. ADD

Similar to COPY but supports URLs and archive extraction.

Example:

ADD app.tar.gz /app/

Most of the time, COPY is preferred.


5. RUN

Executes commands during image creation.

Example:

RUN apt-get update
RUN apt-get install -y nginx

Common use cases:

  • Install packages

  • Configure software

  • Download dependencies


6. CMD

Defines the default command executed when the container starts.

Example:

CMD ["nginx", "-g", "daemon off;"]

Only one CMD should exist in a Dockerfile.


7. ENTRYPOINT

Defines the main executable for the container.

Example:

ENTRYPOINT ["python3"]

ENTRYPOINT is harder to override than CMD.


8. EXPOSE

Documents which port the application uses.

Example:

EXPOSE 80

or

EXPOSE 8080

9. ENV

Defines environment variables.

Example:

ENV APP_ENV=production

10. USER

Specifies which user should run the container.

Example:

USER appuser

Running containers as non-root users improves security.


Python Application Example

FROM python:3.12

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

Nginx Website Example

FROM nginx:latest

COPY . /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Building a Docker Image

Create Image:

docker build -t myapp:v1 .

Explanation:

  • docker build → Build image

  • -t → Tag image

  • myapp:v1 → Image name and version

  • . → Current directory


Running a Container

docker run -d -p 80:80 myapp:v1

Explanation:

  • -d → Detached mode

  • -p → Port mapping


Dockerfile Best Practices

Use Official Images

Good:

FROM python:3.12

Avoid unknown images.


Use Specific Versions

Good:

FROM nginx:1.27

Avoid:

FROM nginx:latest

Minimize Layers

Bad:

RUN apt-get update
RUN apt-get install -y curl

Good:

RUN apt-get update && apt-get install -y curl

Use .dockerignore

Exclude unnecessary files:

.git
node_modules
logs

This reduces image size.


Run Containers as Non-Root

Example:

USER appuser

Improves container security.


Common Dockerfile Mistakes

  • Using large base images

  • Running containers as root

  • Exposing unnecessary ports

  • Hardcoding secrets

  • Using latest tags everywhere

  • Ignoring image optimization


Dockerfile in CI/CD Pipelines

Typical Workflow:

Developer

GitHub

Jenkins

Dockerfile

Docker Build

Docker Image

Docker Hub / Amazon ECR

Kubernetes

Production

Dockerfiles are the foundation of modern containerized CI/CD pipelines.


Dockerfile Interview Questions

What is a Dockerfile?

A Dockerfile is a text file containing instructions used to build Docker images automatically.


What is the difference between COPY and ADD?

COPY only copies files.

ADD can copy files, download URLs, and extract archives.


What is the purpose of CMD?

CMD specifies the default command that runs when a container starts.


What is the difference between CMD and ENTRYPOINT?

CMD provides default arguments.

ENTRYPOINT defines the primary executable.


Why is WORKDIR used?

WORKDIR sets the current working directory inside the container.


Why should we use .dockerignore?

It prevents unnecessary files from being included in Docker images, reducing image size and build time.


Conclusion

Dockerfiles are the backbone of containerized applications. They enable developers to define environments as code, automate image creation, and ensure consistency across development, testing, and production environments.

Understanding Dockerfiles is essential for every DevOps Engineer because they form the foundation of Docker, CI/CD pipelines, Kubernetes deployments, and cloud-native applications.

Comments

Popular posts from this blog

DevOps Roadmap 2026 – Your Complete Beginner-to-Expert Guide

User Management in Linux – Complete Notes for Beginners

Advanced & Scenario-Based CI/CD Interview Questions and Answers