How to Build and Deploy a Static Website Using Docker

Building and deploying a static website can be streamlined using Docker, a popular containerization platform. Docker allows developers to create consistent environments, ensuring that your website works seamlessly across different systems.

Prerequisites

  • Docker installed on your machine
  • Basic knowledge of command-line interface
  • HTML, CSS, and optionally JavaScript files for your website

Step 1: Prepare Your Website Files

Create a folder for your website files. Include your index.html and other assets such as CSS or images. For example:

my-website/

index.html

styles.css

Step 2: Write a Dockerfile

In your website folder, create a file named Dockerfile. This file will define the environment for your static site:

FROM nginx:alpine

COPY . /usr/share/nginx/html

Step 3: Build the Docker Image

Open your terminal, navigate to your website directory, and run:

docker build -t my-static-site .

Step 4: Run the Container

To serve your website locally, execute:

docker run -d -p 8080:80 –name my-site-container my-static-site

Now, open your browser and visit http://localhost:8080 to see your website.

Step 5: Deploy Your Website

Once satisfied, you can deploy your static site to a server or cloud platform. Simply push your Docker image to a container registry like Docker Hub:

docker push yourusername/my-static-site

Then, run it on your hosting environment with Docker installed.

Conclusion

Using Docker to build and deploy a static website simplifies the process, ensuring consistency across environments. With just a few commands, you can develop, test, and deploy your site efficiently.