Seamless Web Deployment: Hosting a HTML Site on Docker

BhGh...2bB9
9 Jan 2024
315

Introduction

Hosting a website doesn't have to be complicated. This guide explains how to deploy a basic HTML website on a Linux server using Docker and make it accessible through your custom domain, http://example.com.

1. Install Docker on Your Linux Server

First, install Docker on your server. The steps will vary slightly depending on your Linux distribution. For Ubuntu, you'd:

  1. Update packages: sudo apt update
  2. Install Docker: sudo apt install docker.io
  3. Check the installation: sudo docker run hello-world

2. Prepare Your Website Content

Create a directory on your server for your web content. It should contain:

  • HTML files and related assets (CSS, JavaScript, etc.).
  • A Dockerfile for building your web server image.

3. Write the Dockerfile

The Dockerfile provides instructions for Docker to build your image. For an HTML website using Nginx, it might look like this:

# Use an official Nginx image 
FROM nginx:alpine 
# Copy static website files to Nginx's public folder 
COPY . /usr/share/nginx/html 
# Expose port 80 
EXPOSE 80 
# Start Nginx when the container launches 
CMD ["nginx", "-g", "daemon off;"] 

This setup uses the lightweight Nginx server to serve your HTML files.

4. Build the Docker Image

In your project directory, build your Docker image:

docker build -t my-html-site

5. Run the Docker Container

Run the Docker container, mapping port 80 of the container to port 80 on the host:

docker run -d -p 80:80 my-html-site 

6. Domain Configuration

To link your domain http://example.com to your server:

  • Point Your Domain to Server IP: Configure your domain's DNS settings (from your domain registrar's panel) to point to your server's IP address.
  • Server Configuration: If you have multiple domains or applications on your server, ensure your Nginx configuration on the Docker container is set to respond to your domain. This usually involves setting up a server block (or virtual host) in Nginx.

7. Access Your Website

After DNS propagation, your website should be accessible at http://example.com.

Docker Commands Cheat Sheet

Here are some helpful Docker commands:

  • List Images: docker image ls
  • Remove Image: docker image rm [image name]
  • List Containers: docker ps -a
  • Stop Container: docker stop [container name]
  • Remove Container: docker rm [container name]

Conclusion

This guide shows how to deploy a simple HTML website using Docker on a Linux server and make it accessible via a custom domain. By following these steps, you can enjoy the simplicity and efficiency of Dockerized web hosting, tailored to your unique domain.
References:
Docker Installation on Linux:

Creating and Configuring a Dockerfile:

Setting Up a Web Server Using Nginx in Docker:

Docker Commands and Management:


BULB: The Future of Social Media in Web3

Learn more

Enjoy this blog? Subscribe to tfntrn

15 Comments