Writing Your First Dockerfile (Beginner-Friendly Guide with Examples and Syntax)

A Dockerfile is one of the most important concepts in Docker. It allows you to create your own custom Docker images instead of using ready-made ones.

Think of a Dockerfile like a recipe. Just like a recipe tells how to cook a dish, a Dockerfile tells Docker how to build your application environment step by step. :contentReference[oaicite:0]{index=0}

Related internal topics: /docker-images-containers, /docker-architecture, /docker-cli-commands, /docker-compose-guide.

1. Dockerfile Workflow (Simple Understanding)

[ Dockerfile ]
      |
      v
docker build
      |
      v
[ Docker Image ]
      |
      v
docker run
      |
      v
[ Running Container ]

Steps:

  • Write Dockerfile
  • Build image using Dockerfile
  • Run container from image

2. Basic Dockerfile Syntax

Here are the most important instructions you will use:

  • FROM → Base image
  • WORKDIR → Set working directory
  • COPY → Copy files
  • RUN → Execute commands
  • CMD → Run application

Simple Syntax Example

FROM openjdk:17
WORKDIR /app
COPY app.jar app.jar
CMD ["java", "-jar", "app.jar"]

Explanation

  • FROM openjdk:17 → Uses Java 17 base image
  • WORKDIR /app → Sets working folder
  • COPY → Copies file into container
  • CMD → Runs application

3. Step-by-Step Practical Example

Step 1: Create HTML File

<html>
  <body>
    <h1>Hello from Docker!</h1>
  </body>
</html>

Step 2: Create Dockerfile

FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html

Step 3: Build Image

docker build -t my-web-app .

Explanation:

  • -t → Tag (name) of image
  • . → Current folder (build context)

Step 4: Run Container

docker run -d -p 8080:80 my-web-app

Explanation:

  • -d → Run in background
  • -p 8080:80 → Map port

Now open browser:

http://localhost:8080

4. Java Application Example

Suppose you have a Spring Boot JAR file.

Dockerfile

FROM openjdk:17
WORKDIR /app
COPY target/app.jar app.jar
CMD ["java", "-jar", "app.jar"]

Build and Run

docker build -t my-java-app .
docker run -d -p 8080:8080 my-java-app

This runs your Java application inside a container.

5. How Dockerfile Creates Layers

Each instruction creates a layer.

FROM → Base Layer
COPY → Code Layer
RUN → Dependency Layer
CMD → Execution Layer

These layers help in caching and faster builds.

6. Real-World Use Cases

1. Microservices

Each service has its own Dockerfile.

2. CI/CD Pipelines

Automatically build images on code changes.

3. Team Collaboration

Everyone uses same environment.

4. Deployment

Run same image in dev, test, and production.

7. Flow Chart: Dockerfile Execution

Read Dockerfile
      |
      v
Execute Instructions
      |
      v
Create Layers
      |
      v
Build Image
      |
      v
Run Container

8. Common Mistakes to Avoid

1. Wrong File Name

File must be named Dockerfile

2. Using Large Images

Use lightweight images like alpine

3. Wrong Instruction Order

Frequent changes should be at bottom

4. Not Using WORKDIR

Leads to messy file structure

5. Copying Unnecessary Files

Increases image size

9. Interview Notes

What is Dockerfile?

Text file with instructions to build image.

Difference between CMD and ENTRYPOINT?

CMD can be overridden, ENTRYPOINT cannot easily be changed.

What is build context?

Folder used during build (.)

What is layer?

Each instruction creates a layer.

Why Dockerfile important?

Ensures consistent environment.

10. Summary

Dockerfile helps you create custom images by defining instructions step by step. It makes applications portable, consistent, and easy to deploy.

By understanding basic instructions like FROM, COPY, and CMD, you can build and run your own applications inside containers.

Next step: Learn optimization techniques in /docker-image-layers.