Building a Web Page to Serve on Nginx
We will create two files one is index.html and aboutme.html and host these files on a new container running Nginx server.
The file structure after creating the two files will be as follows:
The index.html and aboutme.html contain the following code:
Sample Index.html For Web page for Docker Nginx
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta http-equiv = "X-UA-Compatible" content = "IE=edge" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >NGINX Example</ title > </ head > < body > < h1 >HI!!</ h1 > < h3 >This is NGINX</ h3 > < footer > < a href = "./aboutme.html" >About ME</ a > </ footer > </ body > </ html > |
Explanation: This webpage contains two headers and a footer that has a link to about me page.
aboutme.html
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta http-equiv = "X-UA-Compatible" content = "IE=edge" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >About me</ title > </ head > < body > < h1 >Hello!</ h1 > < h3 >This is ganesh prasad!</ h3 > </ body > </ html > |
Explanation: This page shows an h1 header with “Hello” as text and an h3 header with “This is Ganesh Prasad” as its body.
3. Use the following command to create a docker container that maps the local machine’s port to the port on which nginx serves and mount these two files to a location in the container that nginx uses to host files (/usr/share/nginx/html).
docker run -d -p 8000:80 -v address_to_folder_with_files_locally:/usr/share/nginx/html --name my-nginx-server nginx
Explanation:
- The flag -d is used to run the server in detached mode means in the background.
- -p is used to map the local port to the container’s port where the server is serving.
- -v is used to mount the folder that contains the files to a location in the container that Nginx uses to serve the web pages.
- –name is to provide the name of the container and finally, we provide the image name.
Output:
Visit the address localhost:8000 in your browser and you will see the following:
And on clicking the About ME link takes you to the about me page.
Docker – Container for NGINX
Docker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a container shares the OS kernel with only its binaries/libraries loaded with it. To know more about docker, read Introduction to docker.