Saturday, 9 May 2020

Docker: Nginx container: hello world

In this post, I am going to explain how to run nginx container and serve html pages.

Step 1: Create HTML pages.

index.html
<html>
 <head>
  <title>Hello World</title>
 </head>

 <body>
  <h1>Hello World</h1>
 </body>
</html>

welcome.html
<html>
 <head>
  <title>Welcome</title>
 </head>

 <body>
  <h1>Welcome Dear Customer!!!!!!</h1>
 </body>
</html>

Step 2: Execute the below command to start Nginx container.

docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest

--rm: Remove this container once the command execution done.
-v $(pwd):/usr/share/nginx/html: Mount current working directory to /usr/share/nginx/html folder.
-p 1234:80: Whatever the request comes to localhost 1234 port, redirect it to container port 80.
--name my_nginx: 'my_nginx' is the name of container.
nginx:latest: Pull latest version of nginx.

$docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
54fec2fa59d0: Already exists 
4ede6f09aefe: Pull complete 
f9dc69acb465: Pull complete 
Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Status: Downloaded newer image for nginx:latest

Open the url ‘http://localhost:1234’, you will see the content of index.html file.


Open the url ‘http://localhost:1234/welcome.html’ to see the content of welcome.html file.

Login to the container my_nginx
Using ‘docker exec’ command, you can login to the container my_nginx.

docker exec -it my_nginx /bin/bash

$docker exec -it my_nginx /bin/bash
root@0973badab5e5:/#



Previous                                                    Next                                                    Home

No comments:

Post a Comment