As per the official website of NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

NGINX also has its official image on the Docker hub which can be used to create the container to run the application successfully. In this short article, I’ll show how to create the NGINX container using Docker and bind the docker’s host port with the NGINX server.

To complete this article, you must have Docker installed on your host machine. I’m using the Linux machine on which I have already installed Docker. Considering you have Docker installed on your host machine, so let’s get started and see how to create the NGINX container.

Command to create NGINX container

Run the below command to create the NGINX container from the docker’s host machine:

[root@instance-20191018-2102 ~]# docker container run -d --name nginx01 nginx

The above command will produce the result as per the following image:

1. Create NGINX container

Once a container is created, it will give the unique container id.

Let’s understand the options we have used to create an NGINX container:

-d: The option is used to run the container in the detached mode.

–name: This option is used to provide the name to the container, in this case, nginx01 is the name we have provided to our container. If you don’t use this option, the docker will assign the random alphanumeric string to the container.

nginx: This is the name of the NGINX image.

Command to list all containers

List the container and see the NGINX should be up and running.

Run the below command to see the list of all containers:

[root@instance-20191018-2102 ~]# docker container ls -a
2. List the containers

As per the above image, the NGINX server is running with default port 80 which is configured in the NGINX image Dockerfile.

Now, if you want to access the NGINX server, you must know the IP of the NGINX server too. As I mentioned in the article each container has its own host and IP.

Command to view the property of a container

Run the below command to display the properties of a container and find out the IPAddress from the information:

[root@instance-20191018-2102 ~]# docker container inspect nginx01 | grep IPAddress

The above command will produce the result as per the following image. So the container’s IP is 172.17.0.3.

3. Inspect the container

Let’s understand the options we have used to find out the IP address of the container:

inspect: This command is used to display detailed information of one or more containers. Here we are trying to display the information of container nginx01.

grep IPAddress: This is not the docker command, however, this is Linux specific command to find out any word from the command’s output.

The container’s IP is not exposed to the internet, only the Docker host can resolve this IP. Run the curl command from Docker’s host to access the NGINX server.

[root@instance-20191018-2102 ~]# curl 172.17.0.3:80
4. Access the NGINX server

You can see the docker is running as per the above image.

If you try to hit the http://172.17.0.3:80 URL on the browser it will not be accessible as the container’s IP is internal to Docker’s host. So how to access the Docker’s container from the outside Docker’s host too.

To access the NGINX server from the outside, we’ll bind the Docker’s host machine port to the NGINX default port.

Run the below command to create another NGINX container.

[root@instance-20191018-2102 ~]# docker container run -d --name nginx02 -p 8080:80 nginx

Once a container is created it will give the container’s ID as a result. Since the NGINX image already exists on the local Docker’s host machine so it will not download it from the docker hub again.

The only difference between the first and second commands is we have used the -p option.

-p: This option is used to publish a container’s port(s) to the host. The first parameter (8080) specifies the port in the Docker host, the second parameter (80) is mapped to the port exposed in the container.

Now, run the command to list the container and see the PORTS of the first and second NGINX container:

5. Inspect the container

The second NGINX container (nginx01) PORTS says, any request coming on port 8080, divert it to port 80.

Now, hit the

6. Access NGINX server

Instead of providing the custom port(8080), if you want to assign any available random port to the container, you can do this so. To do this run the following command:

[root@instance-20191018-2102 ~]# docker container run -d --name nginx03 -P  nginx

The difference is, we have used -P option without mapping the port.

-P: This is used to publish all exposed ports to random ports.

Now, run the command to list the container and see the random port (32768) is assigned to NGINX (nginx03) container:

7. List the containers

That’s all about creating the NGINX container.

Further readings

How to create a docker container

Architecture and basic terminologies of Docker

Lifecycle of Docker container

Know about all commands of the Docker container

You can look out the complete video to know how to create the NXINX container:

Subscribe to my YouTube channel to get regular updates.