Nginx

How do I use Nginx Docker?

Nginx is a fast, open-source, and more reliable web server that is used for server-side application development. Nginx server application support to run on many different operating systems. Nginx is very useful for development tasks. Therefore, the Docker container provides support for the Nginx server.

The open-source Docker platform contains a docker engine, a runtime environment that is used to execute, builds, and orchestrates containers. The term we have used in the below article is ‘docker hub’, a hosted service where containerized applications are shared, distributed, and collaborated with the rest of the development community. Dockerized applications are portable to implement on any environment like laptop, VM, cloud, or bare-metal server. The modular components can be reassembled again into the fully-featured applications and consciously do their work in a real-time environment.

We will elaborate in this article on how you can use Nginx Docker and easily set up on your system.

All the below-given steps are implemented on Ubuntu 20.04 Focal Fossa release.

Prerequisites

We have to fulfill the following requirements to complete this article:

  1. You need a sign up for a free docker account where you can receive free public repositories.
  2. Docker should install and locally be running on your system.
  3. You need a root account, or a user can run sudo commands.

For a better understanding of the Nginx docker, you have to perform the following steps:

Step 1: you will pull the Nginx Docker image from Docker Hub. Log in to your Docker Hub account. If you are not registered then, you can register for a free account. Once you logged in to the docker hub, you can search and view the image for nginx, which is given below.

To search Nginx images, type nginx in the search bar and then click on the official Nginx link that will be displayed on the search results.

Step 2: Here, you will see the docker pull command for Nginx. Now, on your docker host, run the following Docker pull command on the terminal to download the Nginx latest image from the docker hub.

$ sudo docker pull nginx

Step 4: Use the below-given command to run the Nginx docker container:

$ docker run -it --rm -d -p 8080:80 --name web nginx


We have used the Nginx server port 8080 on the docker host system. After running the above command, you will see the following output window on browsing the http://localhost:8080 URL. The following displayed output shows that the Nginx docker is working properly.

Example:

Let’s discuss an example. In the below-defined example, we can host a web page on our Nginx docker container. Here, we are creating a new custom HTML web page and then test it by using the Nginx image.
Create a new directory named ‘site-content’. In this director, you will add an html file named ‘index.html’ and let’s include the following line of code in the newly created index.html file.

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Docker Nginx</title>

</head>

<body>

<h2>Hello demo to use Nginx container</h2>

</body>

</html>

Run the below-mentioned Docker command.

$ docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx

For nginx server, we exposed port 80 to port 8080 on the docker host.

Next, you will attach the volume on the container ‘/usr/share/nginx/html’ to the present running directory where the helloworld.html program file is saved.

Now, if you will browse the URL http://localhost:8080/index.html, you will get the below-given output on the displaying window:

Conclusion

Today, we have demonstrated in this article how to use NGINX official docker images. We have described how to set up and use the Nginx Docker. Using some simple steps, you can create new Docker images from available images that make your container easier to manage and control.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.