Introduction

In the world of web development, local development environments are crucial for testing and refining projects before deploying them to production. Docker, a containerization platform, has become a popular choice for creating isolated environments that encapsulate an application and its dependencies. NGINX, a high-performance web server, is often employed to serve web content efficiently. In this guide, we’ll explore how to use NGINX and Docker to host a local site, providing a seamless and isolated environment for development.

Prerequisites

Before we begin, make sure you have Docker installed on your system. You can download it from the official Docker website (https://www.docker.com/).

Step 1: Create a Simple Web Application

For this tutorial, let’s create a basic HTML file as our web application. Create a new directory for your project and add an index.html file with the following content:

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Local Site</title>
</head>
<body>
<h1>Hello, Docker and NGINX!</h1>
</body>
</html>

Step 2: Dockerfile for NGINX

Next, we need to create a Dockerfile to define our NGINX container. Create a new file named Dockerfile in the same directory as your index.html file:

Dockerfile
# Use the official NGINX base image
FROM nginx:latest
# Copy the local site files to the NGINX web root
COPY ./ /usr/share/nginx/html

This Dockerfile starts with the official NGINX image and copies the contents of our local directory to the NGINX web root.

Step 3: Build the Docker Image

Open a terminal, navigate to your project directory, and build the Docker image using the following command:

bash
docker build -t local-nginx-site .

This command tells Docker to build an image tagged as local-nginx-site using the current directory as the build context.

Step 4: Run the Docker Container

Once the image is built, run a Docker container based on the image:

bash
docker run -d -p 8080:80 local-nginx-site

This command runs the container in detached mode (-d), mapping port 8080 on your host machine to port 80 on the container.

Step 5: Access Your Local Site

Open your web browser and navigate to http://localhost:8080. You should see the “Hello, Docker and NGINX!” message from your local site.

Custom NGINX Configuration

If you need to customize NGINX settings, you can provide a custom configuration file. Create an nginx.conf file in your project directory with your desired configurations. For example:

nginx
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}# Additional NGINX configurations go here
}

Update your Dockerfile to include the custom configuration:

Dockerfile

FROM nginx:latest

# Copy the custom NGINX configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Copy the local site files to the NGINX web root
COPY ./ /usr/share/nginx/html

Rebuild the Docker image and run the container as before.

Conclusion

Using NGINX and Docker to host a local site provides a flexible and efficient development environment. Docker’s containerization allows for easy management of dependencies, while NGINX ensures high-performance serving of web content. By following the steps outlined in this guide, you can quickly set up a local development environment for your web projects, test them locally, and seamlessly transition to production when ready.