prompter: How would I create a new Nginx docker container?
assistant: 1. Download the official image for NGINX from the docker hub using the following command.

docker pull nginx:latest

This command will fetch the latest version of the nginx image but you can replace “latest” with the version you need and that will be downloaded.

2. Confirm that the image has been downloaded using the following command.

docker images

The result should include nginx along with your other images like this.

3. Now run the docker image using the following command.

docker run -p 8000:80 nginx

Explanation: Using the above command you are running the nginx server where -p is the flag for mapping the port 8000 (local machine) to the port 80 in the container as 80 is the port on which nginx serves by default.

4. Verify the installation by visiting the localhost at the port 8000. We will get the default page for nginx web server as follows:

5. To stop the docker container press CTRL + C.
Host a simple website on NGINX running on Docker container

We will create two files one is index.html and aboutme.html and host these files on a new container running Nginx server.

1. The file structure after creating the two files will be as follows:

2. The index.html and aboutme.html contain the following code:

Index.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

<!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 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 webpages.
    –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:

The home page 

And on clicking the About ME link takes you to the about me page.

The about me page

To stop the docker container you can use the following command.

docker stop my-nginx-server