Sunday, 9 November 2025

Docker - Configure NGINX container

 NGINX (pronounced “engine-x”) is a high-performance web server that can also act as a:

  • Reverse proxy

  • Load balancer

  • HTTP cache

  • Mail proxy (SMTP, POP3, IMAP)

At its simplest, NGINX serves web pages.

Example:
If you put an index.html file in NGINX’s web root directory (/usr/share/nginx/html by default),
and start the service, users can visit your server IP in their browser and see that page.

Install nginx

[root@devopsvm01 ~]# docker pull nginx:latest

latest: Pulling from library/nginx

d7ecded7702a: Pull complete

266626526d42: Pull complete

320b0949be89: Pull complete

d921c57c6a81: Pull complete

9def903993e4: Pull complete

52bc359bcbd7: Pull complete

e2f8e296d9df: Pull complete

Digest: sha256:1beed3ca46acebe9d3fb62e9067f03d05d5bfa97a00f30938a0a3580563272ad

Status: Downloaded newer image for nginx:latest

docker.io/library/nginx:latest

[root@devopsvm01 ~]#

[root@devopsvm01 ~]# docker images

REPOSITORY    TAG       IMAGE ID       CREATED                  SIZE

nginx         latest    d261fd19cb63   Less than a second ago   152MB

ubuntu        latest    97bed23a3497   4 weeks ago              78.1MB

hello-world   latest    1b44b5a3e06a   2 months ago             10.1kB

[root@devopsvm01 ~]#


Start nginx and run in background 

[root@devopsvm01 ~]# docker run --name nginx-c1 -d nginx
31dfa252e7e7b7d72d41909e0eaab6d5fad6f3f0e207cd5766cab25432ca7a2a
[root@devopsvm01 ~]#
[root@devopsvm01 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
31dfa252e7e7   nginx     "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   80/tcp    nginx-c1
[root@devopsvm01 ~]#

--name gives a custom name to container 
-d Runs the container in detached mode (in the background).

But we won't be able to access it , lets remove it and reconfigure it 

[root@devopsvm01 ~]# docker stop 31dfa252e7e7
31dfa252e7e7
[root@devopsvm01 ~]#

[root@devopsvm01 ~]# docker rm nginx-c1
nginx-c1
[root@devopsvm01 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                        PORTS     NAMES
403a136be406   ubuntu    "/bin/bash"   43 minutes ago   Exited (127) 15 minutes ago             awesome_payne
[root@devopsvm01 ~]#


[root@devopsvm01 ~]# docker run --name nginx-c1 -d -p 80:80 nginx
92ba88c6dd1cf78c6e298e7db91c2380233992e27a34670890b46d628e555cb5
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint nginx-c1 (7712ccd21d5e3c1c2c155a7b36d9c73299559f7dd27bb7f6367bcbc8d26114d                              c): failed to bind host port for 0.0.0.0:80:172.17.0.2:80/tcp: address already in use

Run 'docker run --help' for more information
[root@devopsvm01 ~]#

That means your host machine’s port 80 is already occupied, here I'm running  Apache HTTPD on my port 80. You can run NGINX on another port (like 8080 or 8081) and still forward to container’s internal port 80.


[root@devopsvm01 ~]# docker run --name nginx-c1 -d -p 8080:80 nginx
51f299f769e8247869b77b119885c3ec8bb140b0e90babf64204251d4315c92c
[root@devopsvm01 ~]#
[root@devopsvm01 ~]#

Access the url  http://localhost:8080 from your docker host machine .




















We can alter the state of the container using commands like,

[root@devopsvm01 ~]#docker stop nginx-c1

[root@devopsvm01 ~]#docker start nginx-c1

[root@devopsvm01 ~]#docker restart nginx-c1

[root@devopsvm01 ~]#docker pause nginx-c1

[root@devopsvm01 ~]#docker unpause nginx-c1


No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...