DEV Community

Cover image for Deploying a Static Website with Docker and Nginx
Chidera Enyelu
Chidera Enyelu

Posted on

Deploying a Static Website with Docker and Nginx

If you're looking for a fast and lightweight way to serve a static website, Nginx and Docker make an unbeatable combination. In this post, I’ll walk you through how to Dockerize a basic HTML/CSS website using Nginx on an Alpine Linux base image.

Project Structure
Using the command below, clone the static app into your local machine. Here’s what your project folder might look like:

git clone https://github.com/techie-dera/ec2resume.git 
Enter fullscreen mode Exit fullscreen mode

EC2-Demo/

├── index.html
├── style.css
└── Dockerfile
The index.html and style.css files are your actual static website content, and the Dockerfile is what we’ll use to package and serve the site.

🐋 Dockerfile Breakdown
Here’s the Dockerfile we'll use:

FROM nginx:alpine
RUN rm -rf /usr/share/nginx/html/*
COPY ./index.html /usr/share/nginx/html/
COPY ./style.css /usr/share/nginx/html/
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

Image description
What This Does:

FROM nginx:alpine: Uses a minimal Nginx image based on Alpine Linux.
RUN rm -rf /usr/share/nginx/html/: Removes the default web page.
COPY: Adds your index.html and style.css files to the Nginx web root.
EXPOSE 8080: Makes the container’s HTTP port available to the host.
CMD: Keeps Nginx running in the foreground.

Build the Docker Image
Run this command in the directory with your Dockerfile:

docker build -t my-static-app .

Run the Container
After building the image, launch it with:

docker run -d -p 8080:80 --name static-web my-static-app
Visit http://localhost (or your server's IP address if hosted on EC2) to view your site live.

Image description

Image description

Why This Is Great

  • Lightweight: Alpine + Nginx is fast and efficient.
  • Portable: You can deploy the same container anywhere — EC2, ECS, or Kubernetes.
  • Simple: No web servers to install manually or configure.

Top comments (0)