Introduction
Nginx is an open source web server used for serving static or dynamic websites, reverse proxying, load balancing, and other HTTP and proxy server capabilities. Designed to handle large numbers of simultaneous connections, it is a popular web server thathosts some of the largest and busiestwebsites on the internet.
Docker is a popular open-source containerization tool thatprovides a portable and consistent runtime environment for software applications while consuming fewer resources than a traditional server or virtual machine. Docker uses containers, isolated user-space environments that run at the operating system level and share system resources such as the kernel and filesystem.
By including Nginx, it is possible to reduce the system administration.
Pre-requisites
Ubuntu Server 22.04 on host or virtual machine.
Docker installed on your ubuntu server
Step 1 – Download Nginx From Docker Hub
Docker maintains a site called Dockerhub, a public repository of Docker files that include both official and user-submitted images.
sudo docker pull nginx
Nginx installed, you can configure the container so that it’s publicly accessible as a web server.
sudo docker run --name docker-nginx -p 80:80 nginx
Description on the used commands
run is the command to create a new container
--name flag is how you specify the name of the container.
-p specifies the port you are exposing in the format of -p local-machine-port:internal-container-port.
:80 port in the container to port :80 on the server.
nginx is the name of the image on Docker Hub.
In a web browser, enter server’s IP address to start Nginx’s
In terminal you can verify the container status with this command:
sudo docker ps -a
If you wish to remove the existing container
sudo docker rm docker-nginx
Step 2 – Running in Detached mode
Create a new, detached Nginx container
sudo docker run --name docker-nginx -p 80:80 -d nginx
sudo docker ps
Stopping the container
sudo docker stop docker-nginx
Once the container is stop you can remove it.
sudo docker rm docker-nginx
Step 3 – Build a Web Page to Serve on Nginx
Let us create a custom page. so we create a new diretory
sudo mkdir -p ~/docker-nginx/html
cd ~/docker-nginx/html
Create a HTML file using nano text editor
sudo nano index.html
<html>
<head>
<title>Docker nginx </title>
</head>
<body>
<div class="container">
<h1>Hello </h1>
<p>This Nginx page is brought to you by Docker </p>
</div>
</body>
</html>
Step 4 — Link the Container to your Local system
We will link Nginx to container so that it is publicly accessible over port:80
and can connect to the website.
The Nginx container is set up by default to look for an index page at /usr/share/nginx/html
. In your new Docker container, you will need to give it access to your files at that location. To do this, use the -v
flag to map the ~/docker-nginx/html
folder from your server to a relative path in the container/usr/share/nginx/html
.
docker run --name docker-nginx -p 80:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx
Command Description:
-v
flag specifies that you’re linking a volume.- It is the location of your directory on your server,
~/docker-nginx/html
. - It is the location that you are symbolically linking to your container
/usr/share/nginx/html
- After running that command, enter your server’s IP address into your browser to view your new landing page. You can upload more content to the
~/docker-nginx/html/
directory, and it will be added to your live website.if you modify your HTML file, and refresh your browser, it will be updated accordingly.
Step 5 : Using your own NGINX configuration File
cd ~/docker-nginx
Copy the Nginx config directory into your project folder
docker cp docker-nginx:/etc/nginx/conf.d/default.conf default.conf
Now rebuild the container. So stop it first and then remove it,
sudo docker stop docker-nginx
sudo docker rm docker-nginx
Now edit the default Nginx configuration file locally to serve a new directory, or to use a proxy_pass
to forward the traffic to another application or container.
sudo docker run --name docker-nginx -p 80:80 -v ~/docker-nginx/html:/usr/share/nginx/html -v ~/docker-nginx/default.conf:/etc/nginx/conf.d/default.conf -d nginx
This command links the custom website pages to the container and then restart the container.
sudo docker restart docker-nginx
Step 6 Finale
Hooray, now you have a running Nginx container serving a custom web page and now you can configure Nginx from within your container.