Deploying Your Static Website with Ease using NGINX

Tony Alosius
7 min readAug 31, 2023

--

First lets understand the fundamentals of docker,

What is Docker?

Docker is a platform that enables developers to develop, package, and deploy applications as lightweight containers. Containers provide an isolated and consistent environment for running applications, ensuring that the software runs consistently across different environments, from development to production.

What is Docker Hub?

Docker Hub is a cloud-based repository service provided by Docker. It allows users to store, share, and manage Docker images. Docker Hub hosts a vast collection of public Docker images that you can use as a starting point for your own containers. Additionally, you can use Docker Hub to publish and share your own Docker images with others.

What is Docker Image?

A Docker image is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. Images are created based on a Docker file, which contains instructions for building the image. Images are read-only and serve as the blueprint for creating Docker containers.

An important distinction to be aware of when it comes to images is the difference between base and child images.

  • Base images are images that have no parent image, usually images with an OS like ubuntu, busybox or debian.
  • Child images are images that build on base images and add additional functionality.

Then there are official and user images, which can be both base and child images.

  • Official images are images that are officially maintained and supported by the folks at Docker. These are typically one word long. In the list of images above, the python, ubuntu, busybox and hello-world images are official images.
ubuntu: Provides a minimal Ubuntu-based Linux distribution.
node: Offers Node.js runtime for building JavaScript applications.
python: Provides the Python runtime and libraries.
nginx: Offers the Nginx web server.
mysql: Provides the MySQL database server.
postgres: Offers the PostgreSQL database server.
mongo: Provides the MongoDB NoSQL database server.
  • Community images: The Docker community maintains a vast collection of images for various software and services. These images are usually contributed by individuals, organizations, or open-source projects. You can find community images on Docker Hub (https://hub.docker.com/) or other container registries. Some popular community images include:
alpine: A minimal Linux distribution known for its small size and security.
busybox: A lightweight image that provides a minimal shell environment.
debian: Provides a minimal Debian-based Linux distribution.
golang: Offers the Go programming language runtime.
redis: Provides the Redis in-memory data store.
elasticsearch: Offers the Elasticsearch search and analytics engine.
  • Custom base images: Besides the official and community images, you can create your own custom base images. These images are built by defining a Dockerfile with your desired configurations, dependencies, and software stacks. By starting from a base image and adding your application-specific layers, you can create custom images tailored to your specific needs.
  • User images are images created and shared by users like you and me. They build on base images and add additional functionality. Typically, these are formatted as user/image-name.

What is Docker Container?

A Docker container is a runnable instance of a Docker image. Containers are isolated environments that share the host operating system’s kernel but have their own filesystem, processes, and network. Containers can be started, stopped, and managed using Docker commands. They provide a consistent runtime environment, ensuring that applications run the same way regardless of the host system.

What is a Docker Web Server?

A “Docker web server” refers to a web server application that is packaged and deployed within a Docker container. This containerized web server allows you to isolate, distribute, and run your web applications in a consistent and portable environment. Docker web servers streamline the process of setting up, configuring, and managing web servers by encapsulating the server software and its dependencies into self-contained units called containers. This approach enhances development, deployment, and scalability while ensuring consistent behavior across different environments.

There are several web servers available in Docker that you can use to serve your web applications. Here are some popular ones:

  1. Nginx: Nginx is a widely used, high-performance web server and reverse proxy server. It’s known for its efficiency and can handle high levels of concurrent connections. Nginx is often used as a load balancer and for serving static content.
  2. Apache HTTP Server (Apache): Apache is one of the most well-known and widely used web servers. It’s highly configurable and supports a wide range of modules for various functionalities.
  3. Microsoft Internet Information Services (IIS): IIS is a web server developed by Microsoft for Windows-based environments. It’s often used for hosting ASP.NET applications and websites built using Microsoft technologies.
  4. Caddy: Caddy is a modern, easy-to-use web server that includes automatic HTTPS by default using Let’s Encrypt. It’s designed to be user-friendly and efficient.
  5. Lighttpd: Lighttpd (pronounced “lighty”) is a lightweight web server known for its speed and efficiency. It’s suitable for serving static content and handling low-resource environments.

Docker Workflow:

Creating A Docker File for Own Static Website

Step 1: Prepare your static website files Create a directory on your local machine and place all the files of your static website inside it. For demonstration purposes, let’s assume your website files are located in a directory called “my-static-website.”

Step 2: Create a Dockerfile In the same directory where your website files are located, create a file named “Dockerfile” (without any file extension) using a text editor.

Step 3: Add the following content to the Dockerfile:

# Use a lightweight base image
FROM nginx:alpine

# Copy the static website files to the Nginx document root
COPY . /usr/share/nginx/html

Step 4: Build the Docker image Open a command prompt or terminal and navigate to the directory where your website files and Docker file are located. Run the following command to build the Docker image:

docker build -t myportfolio .

This command tells Docker to build an image with the tag “my-static-website-image” using the current directory (containing your website files and Docker file) as the build context. The dot “.” at the end represents the current directory.

Step 5: Verify the Docker image After the build process completes successfully, you can verify that the Docker image was created by running the following command:

docker images

You should see the “myportfolio” listed among the images.

Step 6: Run the Docker container To run the Docker container based on your newly created image, execute the following command:

docker run -d -p 80:80 myportfolio

This command starts a container based on the “myportfolio” maps port 80 of the container to port 80 of the host machine, and runs the container in detached mode (“-d”).

Step 7: Access your static website You should now be able to access your static website by opening a web browser and entering “http://localhost" or “http://<your_host_ip_address>” in the address bar. The web server running in the Docker container will serve your website.

Creating a Docker Hub registry

To push a Docker image to the Docker Hub registry, you’ll need to follow these steps:

1.Tag your Docker image: Before pushing the image, you should tag it with the appropriate name and version. The naming convention for Docker Hub is username/repository:tag. Replace username with your Docker Hub username or organization name, repository with the name of your repository, and tag with the desired version or tag for your image.

docker tag local-image:tagname username/repository:tagname

For example, if your Docker Hub username is “johnsmith” and your repository name is “my-static-website-image,” and you want to tag it as “v1.0,” the command would look like this:

docker tag my-static-website-image:v1.0 tonyalosius10/myportfolio:v1.0
docker tag my-static-website-image:latest tonyalosius10/myportfolio:v1.0

2. Log in to Docker Hub: Before pushing the image, you need to log in to your Docker Hub account using the docker login command. This command prompts you for your Docker Hub username and password.

docker login

Enter your Docker Hub username and password when prompted.

3. Push the Docker image: After tagging the image and logging in, you can push the image to Docker Hub using the docker push command.

docker push username/repository:tagname

Continuing with the previous example, you would run:

docker push tonyalosius10/myportfolio:v1.0

This command pushes the tagged image to the Docker Hub registry under your specified repository and tag.

4. Wait for the push to complete: The push command uploads the image and its layers to Docker Hub. The time it takes depends on the size of the image and your internet connection. Once the push is complete, you’ll receive a success message indicating that the image was pushed successfully.

5. Verify the pushed image: You can go to the Docker Hub website (https://hub.docker.com) and sign in with your Docker Hub account to verify that the image has been pushed successfully. You should be able to see your repository and the pushed image with its corresponding tag.

If you enjoyed this article, share it with your friends and colleagues! Leave your valuable comments, feedback and thoughts so that as a young blogger it would be of a great help and support for me move ahead. Hope I have made your time valuable.

--

--