This repository provides a guide on how to create a Docker image to host an HTML website using Nginx. It includes an example Dockerfile and instructions for building and running the Docker container. You can use any HTML template, such as those available on Free CSS Templates.
Before you begin, ensure you have the following installed on your local machine:
- Ensure you replace with your actual Docker Hub username in the commands.
- For security purposes, avoid using the latest tag in production environments.
- specify a specific version number instead.
- Visit Free CSS Templates and browse through the available templates.
- Choose a template you like and download it as a ZIP file.
- Extract the contents of the ZIP file to a folder on your local machine.
-
Create a new directory for your project:
mkdir html-website-docker cd html-website-docker -
Inside this directory, create a subdirectory named
websiteand move the extracted HTML template files into this folder:mkdir website mv /path/to/extracted-html-template/* website/ -
Your project directory structure should look like this:
html-website-docker/ ├── Dockerfile └── website/ ├── index.html ├── about.html ├── css/ ├── js/ ├── images/ └── ... (other HTML/CSS/JS files and assets)
Create a Dockerfile in the html-website-docker directory with the following content:
# Use the official Nginx image as the base image
FROM nginx:alpine
# Copy the HTML website files to the Nginx directory
COPY website/ /usr/share/nginx/html
# Expose port 80 to the outside world
EXPOSE 80Run the following command to build the Docker image. Replace yourdockerhubusername with your Docker Hub username.
docker build -t yourdockerhubusername/html-website:latest .This command will create a Docker image tagged as yourdockerhubusername/html-website:latest.
To run the Docker container and expose it on port 8080 of your local machine, use the following command:
docker run -d -p 8080:80 yourdockerhubusername/html-website:latestThis command will run the Docker container in detached mode (-d) and map port 80 inside the container to port 8080 on your host machine.
Open a web browser and navigate to http://localhost:8080 to view your HTML website hosted by Nginx.
If you want to share your Docker image, push it to Docker Hub using the following command: Login to your Dockerhub Account:
docker loginPush the image:
docker push yourdockerhubusername/html-website:latestOthers can pull and run your Docker image from Docker Hub using:
docker pull yourdockerhubusername/html-website:latest
docker run -d -p 8080:80 yourdockerhubusername/html-website:latestYou have successfully created and run a Docker container that serves an HTML website using Nginx. Feel free to experiment with different HTML templates from Free CSS Templates or customize the Dockerfile to suit your needs!