Docker - Container for NGINX
Last Updated :
15 Jul, 2024
Docker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a container shares the OS kernel with only its binaries/libraries loaded with it. To know more about docker, read Introduction to docker.
What is NGINX Server?
Nginx is mostly used as reverse proxy software. Nginx is an open-source web server you can perform multiple tasks with the nginx like scalability, efficiency, and flexibility it is specially designed for high performance which can be handled in scenarios where there is a high amount of incoming requests.
Benefits of Nginx Server
The following are the benefits of a Nginx server:
- High Performance: Nignix is an event-driven architecture that can handle high loads at a time wit more efficiently. Nginx will consume very little memory which will help in improving overall efficiency.
- Scalability: Nginx can be scaled up and down depending on the incoming traffic. To manage growing workloads, it can be installed on single servers or clusters of servers.
- Flexibility: Nginx is very flexible to use apart from the web serving it can also be used for other things like reverse proxy, load balancer, caching server, and media streaming server.
- Low Resource Consumption: Nginx consumes very less amount of resources like CPU and Memory when compared to the other web servers.
- Open Source and Community Support: Nginx is an open-source platform so its users are very huge so the documentation, tutorials, and support are available for new users and students if they want to learn.
What are Docker Containers and Why Use Them For NGINX?
Containerization is OS-based virtualization that creates multiple virtual units in the userspace, known as Containers. Containers share the same host kernel but are isolated from each other through private namespaces and resource control mechanisms at the OS level. Container-based Virtualization provides a different level of abstraction in terms of virtualization and isolation when compared with hypervisors. Hypervisors use a lot of hardware which results in overhead in terms of virtualizing hardware and virtual device drivers. A full operating system (e.g -Linux, Windows) runs on top of this virtualized hardware in each virtual machine instance. Dockers provide a consistent and isolated environment for running the application, making them a popular choice for deploying the Nginx.
Benefits of Using Docker Containers for NGINX
The following are the benefits of using Docker containers for nginx server:
- You can the nginx in the different types of underlying OS platforms which makes it light in weight and portable for different environments even though you change the nginx container from one platform the behavior will also remain the same in each and every platform.
- Docker container completely isolates nginx container this conflicts will be reduces because of the isolation it will reduces the risk of security it will stop the affecting the other applications.
- Containers are light in weight there will share the host system kernel which make them more efficient when compared to others like virtual machine and son on.
- Docker is widely adopted in the DevOps methodology which make it easier to integrate the nginx deployment into continuous integration and continuous delivery (CI/CD) pipelines.
What Is Docker Nginx?
When a user requests a page from a web server, the web server takes the request and sends an appropriate response back to the user. Nginx can be that web server. NGINX is an open-source web server that is also used for reverse proxy, HTTP load balancing, and email proxy. It is very efficient in using the system's resources and can handle a huge number of simultaneous requests using event-driven and asynchronous architecture. That is the reason why Nginx is an excellent choice for websites that deal with high loads like e-commerce, cloud storage, and search engines. To know more about Nginx refer to What is Nginx (Web Server) and how to install it?
How To Pull Docker Nginx Image?
Follow the steps mentioned below to pull the Nginx image by using Docker.
Step 1: Open the Docker server where it was installed.
Step 2: You can pull the docker nginx image type depending on the requirement of your organization like you use the community version or enterprise version depending on the requirement of the organization.
Step 3: By using the command mentioned below you can pull the nginx image by using Docker which is located in the DockerHub.
docker pull nginx:<Version>
Docker pull is a command that is used to pull any image you want to use and Nginx is the image that you can run as a container <version> you have to mention the version of the image based on your requirement. If you didn't mention any version then docker will automatically pull the latest version of the image.You can write your own Dockerfile to create a customized Docker Nginx image
Why Docker Containers?
One common problem that most developers face is when an application runs on one machine but doesn't on another. This can be due to different OS and different versions of libraries like a developer developed an application using nodejs 14.1 but the cloud instance has nodejs 9.2 installed.This is the exact problem that a docker container solves, it packages the app's libraries and all the dependencies, prebuilt and ready to be executed. It is isolated from other containers and makes the application feel that it is the only application running on the system.Many organizations are now moving to containers from virtual machines as containers are lightweight and easy to maintain using the provided CLI.It also helps in an easy adaptation of microservices architecture moving away from traditional monolithic systems. Other benefits include scalability, modification, and maintenance.
Docker Images And Docker Hub
A docker image is just like a snapshot in VM environments. It records information about the docker container at a specific time like all the libraries along with their particular versions needed for an application. It is immutable but can be easily duplicated and shared with others.An image is usually shared with others in order to enable someone else to run the application in the same environment it is supposed to run and the image holds all the information about that environment.A Docker hub is one such platform where you can find and share container images with others. Some of the most common images are for Nginx, Nodejs, Mongo DB, and many more.
How To Run Docker Nginx Image In Detached Mode?
You can run Docker nginx image in by using following commad. But when you press CTRL +C then containers stops running because you are running the Docker container in the form of Attached mode when exit the container will stop running automatically.
docker run nginx
- To over come the above stitutation you need to run the containers in the form of detached mode. You can do that by using following command.
docker run -d nginx
-d indiactes that you are running the image in the form of detachd mode. To Know more about Docker Commands you can refer to Docker – Instruction Commands.
How To Push The Docker Nginx Image To DockerHub?
- After pulling the image form the DockerHub you can make some changes to the image according to your byssienss operations
Downloading The Nginix From Docker Hub
- 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.
How To List The Docker Images?
- 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.

How to create and run an NGINX container with Docker?
- Now run the docker image using the following command.
docker run -p 8000:80 nginx
- 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.Verify the installation by visiting the localhost at the port 8000.
- We will get the default page for nginx web server as follows:

Building a Web Page to Serve on Nginx
- We will create two files one is index.html and aboutme.html and host these files on a new container running Nginx server.
- The file structure after creating the two files will be as follows:

- The index.html and aboutme.html contain the following code:
Sample Index.html For Web page for Docker Nginx
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
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 the 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 web pages.
- --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:
- And on clicking the About ME link takes you to the about me page.

How To Stop To Docker Nginx Container?
- To stop the docker container you can use the following command. After completation
docker stop my-nginx-server
- Stop is the command used to some the containers which are running and "my-nginx-server" is the name of the container.
Customizing NGINX configuration in Docker containers
- Follow the steps mentioned below to crate an coustm nginx images.
Step 1: Create a Custom NGINX Configuration File
- Create an coustm nginx file like example GFG.conf as following.
# custom.conf
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://your_backend_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Add other proxy settings as needed
}
# Add other server block configurations as needed
}
Step 2: Create A Dockerfile for Nginx as follows.
# Use the official NGINX base image
FROM nginx:latest
# Copy the custom NGINX configuration file to the container
COPY <Path>
Step 3: Build the docker image using the dockerfile with the following command
docker build -t <Nmae of the file> .
Step 4: Run the Docker image as an Container
- The following command helps in running the docker image as a container:
docker run -p 8080:80 <Name of the Image>
Advanced Docker concepts for NGINX management
In docker advanced concepts you can use docker more efficiently can deploy the application with more feature and security. Following was the concepts of nginx deployment in docker containers.
- Volume Mounts for Persistent Data.
- Networking Configuration for Multi-Container Applications.
- Health Checks for Container Monitoring.
- Image Building for Customized Deployments.
- Resource Management for Scalability and Performance.
- Orchestration Tools for Large-Scale Deployments.
- Continuous Integration and Continuous Delivery (CI/CD).
Conclusion
In this article, we learned in brief about docker, container, images, and docker hub. Then we created a docker container using the official Nginx image from the docker hub and ran it. We also learned how we can list all the images on our system and how to stop a docker container. After that, we hosted a small webpage on the Nginx server running on a docker container.
Similar Reads
What is Docker? Have you ever wondered about the reason for creating Docker Containers in the market? Before Docker, there was a big issue faced by most developers whenever they created any code that code was working on that developer computer, but when they try to run that particular code on the server, that code
12 min read
Introduction to Docker
Docker Installation
Docker - Installation on WindowsIn this article, we are going to see how to install Docker on Windows. On windows if you are not using operating system Windows 10 Pro then you will have to install our docker toolbox and here docker will be running inside a virtual machine and then we will interact with docker with a docker client
2 min read
How to Install Docker using Chocolatey on Windows?Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Commands
Docker Images
What is Docker Image?Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the
10 min read
Working with Docker ImagesIf you are a Docker developer, you might have noticed that working with multiple Docker Images at the same time might be quite overwhelming sometimes. Managing numerous Docker Images all through a single command line is a very hefty task and consumes a lot of time. In this article, we are going to d
2 min read
Docker - Publishing Images to Docker HubDocker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
Docker CommitDocker is an open-source container management service and one of the most popular tools of DevOps which is being popular among the deployment team. Docker is mostly used in Agile-based projects which require continuous delivery of the software. The founder, Chief Technical Officer, and Chief Archite
10 min read
Docker - Using Image TagsImage tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read
Next.js Docker ImagesUsing Next.js Docker images allows your app to deploy to multiple environments, and is more portable, isolated and scalable in dev and prod. Dockerâs containerization makes app management super easy, you can move from one stage to another with performance.Before we get started, letâs cover the basic
14 min read
How to Use Local Docker Images With Minikube?Minikube is a software that helps in the quick setup of a single-node Kubernetes cluster. It supports a Virtual Machine (VM) that runs over a docker container and creates a Kubernetes environment. Now minikube itself acts as an isolated container environment apart from the local docker environment,
7 min read
Docker Containers
Containerization using DockerDocker is the containerization platform that is used to package your application and all its dependencies together in the form of containers to make sure that your application works seamlessly in any environment which can be developed or tested or in production. Docker is a tool designed to make it
9 min read
Virtualisation with Docker ContainersIn a software-driven world where omnipresence and ease of deployment with minimum overheads are the major requirements, the cloud promptly takes its place in every picture. Containers are creating their mark in this vast expanse of cloud space with the worldâs top technology and IT establishments re
8 min read
Docker - Docker Container for Node.jsNode.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
12 min read
Docker - Remove All Containers and ImagesIn Docker, if we have exited a container without stopping it, we need to manually stop it as it has not stopped on exit. Similarly, for images, we need to delete them from top to bottom as some containers or images might be dependent on the base images. We can download the base image at any time. So
10 min read
How to Push a Container Image to a Docker Repository?In this article we will look into how you can push a container image to a Docker Repository. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository:Step 1: Create a Docker Account The f
3 min read
Docker - Container LinkingDocker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers.There are times during the development of our application when we need two containers to be able to communicate with each other. It might be p
4 min read
How to Manage Docker Containers?Before virtualization, the management of web servers and web applications was tedious and much less effective. Thanks to virtualization, this task has been made much easier. This was followed by containerization which took it a notch higher. For network engineers, learning the basics of virtualizati
13 min read
Mounting a Volume Inside Docker ContainerWhen you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers
10 min read
Difference between Docker Image and ContainerPre-requisite: Docker Docker builds images and runs containers by using the docker engine on the host machine. Docker containers consist of all the dependencies and software needed to run an application in different environments. What is Docker Image?The concept of Image and Container is like class
5 min read
Difference between Virtual Machines and ContainersVirtual machines and Containers are two ways of deploying multiple, isolated services on a single platform. Virtual Machine:It runs on top of an emulating software called the hypervisor which sits between the hardware and the virtual machine. The hypervisor is the key to enabling virtualization. It
2 min read
How to Install Linux Packages Inside a Docker Container?Once you understand how to pull base Docker Images from the Docker registry, you can now simply pull OS distributions such as Ubuntu, CentOS, etc directly from the Docker hub. However, the OS Image that you have pulled simply contains a raw file system without any packages installed inside it. When
2 min read
Copying Files to and from Docker ContainersWhile working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive
9 min read
How to Run MongoDB as a Docker Container?MongoDB is an open-source document-oriented database designed to store a large scale of data and allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables. In this
4 min read
Docker - Docker Container for Node.jsNode.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
12 min read
Docker - Container for NGINXDocker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a
11 min read
How to Provide the Static IP to a Docker Container?Docker is an open-source project that makes it easier to create, deploy and run applications. It provides a lightweight environment to run your applications.It is a tool that makes an isolated environment inside your computer. Think of Docker as your private room in your house. Living with your fami
2 min read
Docker Compose
Docker Swarm
Docker Networking
Docker NetworkingPre-requisite: Docker Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help
5 min read
Docker - Managing PortsPre-requisites: Docker Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the c
4 min read
Creating a Network in Docker and Connecting a Container to That NetworkNetworks are created so that the devices which are inside that network can connect to each other and transfer of files can take place. In docker also we can create a network and can create a container and connect to the respective network and two containers that are connected to the same network can
2 min read
Connecting Two Docker Containers Over the Same NetworkWhenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
How to use Docker Default Bridge Networking?Docker allows you to create dedicated channels between multiple Docker Containers to create a network of Containers that can share files and other resources. This is called Docker Networking. You can create Docker Networks with various kinds of Network Drivers which include Bridge drivers, McVLAN dr
7 min read
Create your own secure Home Network using Pi-hole and DockerPi-hole is a Linux based web application, which is used as a shield from the unwanted advertisement in your network and also block the internet tracking system. This is very simple to use and best for home and small office networks. This is totally free and open-source. It also allows you to manage
3 min read
Docker Registry