Beginner10 min readUpdated Jan 2025

Build an NGINX Webserver Using Dockerfile (Beginner-Friendly Visual Guide)

A simple, visual, beginner-friendly guide showing how to build an NGINX webserver using a Dockerfile. Learn how Dockerfile works, how NGINX serves files, and how to run your webserver as a Docker container.


1. Before Starting: What Are We Actually Building?

Let’s keep it simple:

πŸ‘‰ We will build a fully working NGINX webserver inside a Docker container. πŸ‘‰ It will serve a static webpage (index.html). πŸ‘‰ You can access it on your browser at:

http://localhost:8080

This project teaches:

  • What a Dockerfile does
  • How NGINX works inside a container
  • How to copy files into a container
  • How to expose ports
  • How to run a clean production-grade webserver

Even a beginner can follow everything.


2. Visual Understanding: How NGINX Works in Docker

Here is the simple idea:

Your HTML File  
        ↓  
Dockerfile  
        ↓ docker build
NGINX Image + Your Files  
        ↓ docker run
Running Webserver on localhost:8080

Think of Docker as a box that contains:

  • NGINX engine
  • Your website files
  • Instructions on how to run NGINX

Visual Diagram:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  NGINX Container             β”‚
β”‚   β”œβ”€β”€ /usr/share/nginx/html β”‚ ← your website
β”‚   └── NGINX webserver       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚ Port mapping
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Your Browser (localhost)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

3. Step 1β€” Create Project Structure

nginx-docker-project/
β”‚
β”œβ”€β”€ index.html
└── Dockerfile

index.html

Create a very simple page:

<h1>Hello from NGINX running inside Docker πŸš€</h1>

4. Step 2- Create the Dockerfile

(Everything explained before using)

Here is your Dockerfile:

# 1. Start with official NGINX image
FROM nginx:alpine

# 2. Copy your website files into NGINX default folder
COPY index.html /usr/share/nginx/html/index.html

# 3. Expose port 80 (NGINX default)
EXPOSE 80

Visual Breakdown:

FROM nginx:alpine
↓
Borrow a ready-to-use lightweight NGINX webserver

COPY index.html ...
↓
Replace default homepage with your file

EXPOSE 80
↓
Tell Docker this server listens on port 80

That’s your entire recipe.


5. Step 3 β€” Build the Image

Run:

docker build -t nginx-example .

Visual:

Dockerfile β†’ nginx-example:latest (your image)

πŸš€ 6. Step 4 β€” Run Your NGINX Webserver

Run:

docker run -p 8080:80 nginx-example

Visual Explanation:

8080 (your laptop)
β†’
80 (inside container)

Open browser:

http://localhost:8080

You will see:

Hello from NGINX running inside Docker πŸš€

Your first webserver is live!


7. Step 5 β€” Make It Even Better (Optional HTML Folder Support)

If you have a folder:

/site
   β”œβ”€β”€ index.html
   β”œβ”€β”€ about.html
   β”œβ”€β”€ styles.css

Use:

FROM nginx:alpine
COPY site/ /usr/share/nginx/html/
EXPOSE 80

This copies all your website files inside NGINX.


8. Why NGINX + Docker Is So Powerful

βœ” You get a production-ready webserver βœ” No need to install NGINX manually βœ” Your site becomes portable βœ” Deployment becomes one command βœ” Works on any machine

This is why companies use Docker everywhere.


9. Visual Summary (5 Seconds)

Dockerfile = instructions  
Build = create image  
Run = start webserver  
NGINX serves your static files  
Port mapping connects container β†’ browser  

Done.


**10. FAQs **

Q1. Why use NGINX in Docker instead of installing locally?

Because Docker gives you an isolated, clean environment and is easier for deployment.


Q2. Do I need to write a long Dockerfile for NGINX?

No. NGINX official image is already optimized. You hardly need 3–4 lines.


Q3. Can I serve CSS, JS, and images with this setup?

Yes. Just copy entire folder into /usr/share/nginx/html/.


Q4. Why map port 8080 instead of 80?

Port 80 may require admin rights. 8080 is safer for local development.


Q5. Can this be deployed to cloud servers?

Absolutely β€” the same Docker image will work on any cloud provider.


Final Words

Building an NGINX webserver with Docker is one of the cleanest ways to learn how Docker actually works. You write a tiny Dockerfile β†’ Docker builds the image β†’ the image becomes a real running webserver β†’ and your browser loads the page instantly.

Once you understand this flow, Docker stops feeling complicated. It becomes a simple tool:

β€œTake my code β†’ follow my instructions β†’ run my app anywhere.”

Exactly what Docker was made for.


Mastered this concept?

Take the next step in your journey to becoming a senior developer.