Hosting a static content in Nginx docker

Hosting a static content in Nginx docker

·

3 min read

Introduction

Peace to you all , Here is the Link to my previous blogs , Where I have posted about Docker, Containers and DevOps.

This is Day 4 of documenting my DevOps journey .

This blog focuses on Hosting a static content on Nginx in docker .

Getting Started

In the previous blog , we successfully ran an Nginx web server on docker ,if not just kindly copy paste the below code .

docker pull nginx

You can check whether the image has been pulled by ,

docker images

image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643035294323/mYv7HUAgw.png)
image.png

We also have a static website already ready in our previous blog , if not just kindly copy paste the below code

<h1> This is a sample Web App </h1>

and save it as an HTML file .

Exploring the Nginx web server

Now , We need to find where Nginx has its HTML files in them , in order to change those html file and replace it with our own .

Scrolling down on the Nginx image in docker hub , we can find the instructions on how to host a simple static content .

image.png /some/content should refer to the path of our html file , we just built .

In my case , I have saved the file inside the users in C:, so my command would be ,

docker run --name webapp -d -p 80:80  -v /c/Users/name/dockersample:/usr/share/nginx/html nginx

Breaking down the above command ,

run —> To run an instance of the image mentioned ,

name —> To set name to the container ,

d —> To run the container in detached mode ( runs in the background and we can have the terminal back),

p —> To access ports in container from hosts ,

v —> Setting up the volume from host to container and

Finally the name of the image , here it is nginx.

Running the above command , displays an ID - which is the container ID.

Now we can check whether the container is running by a command ,

docker ps

or

in the docker desktop ,

image.png

We can find that the container is running successfully.

Opening in browser

Once the container starts running , we can open a browser and check on localhost:80 .

image.png !!! We successfully hosted a static content on Nginx and ran the container in docker!

Though it looks simple , Trust me , This is huge .

What have we done

We pulled an image from Docker Hub ,

The image is known for hosting web applications , Nginx,

We had a static content and mounted it to Nginx image’s static web page and successfully ran it.

See you Tomorrow

Okay . So hold on, stay with me as we get to know docker more and DevOps even more.

Okay Then , Will get back Tomorrow with more!

Peace be upon you!

Did you find this article valuable?

Support Shajith by becoming a sponsor. Any amount is appreciated!