How to configure port binding for NGINX container on port 8080?

DockerDockerBeginner
Practice Now

Introduction

In this tutorial, we will explore the process of configuring port binding for an NGINX container on port 8080 using Docker. By the end of this guide, you will have a clear understanding of how to set up and verify your NGINX container deployment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/DockerfileGroup(["`Dockerfile`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/port("`List Container Ports`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/ImageOperationsGroup -.-> docker/images("`List Images`") docker/DockerfileGroup -.-> docker/build("`Build Image from Dockerfile`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/port -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/run -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/start -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/stop -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/pull -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/images -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/build -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} docker/ls -.-> lab-416178{{"`How to configure port binding for NGINX container on port 8080?`"}} end

Introduction to Docker and NGINX

Docker is a popular open-source platform that enables developers to build, deploy, and run applications in containerized environments. NGINX, on the other hand, is a high-performance web server and reverse proxy that is widely used for load balancing, caching, and serving static content.

What is Docker?

Docker is a containerization platform that allows developers to package their applications and dependencies into a single, portable container. This container can then be easily deployed and run on any system that has Docker installed, regardless of the underlying operating system or infrastructure.

What is NGINX?

NGINX is a powerful and versatile web server that is known for its high performance, scalability, and flexibility. It is commonly used as a reverse proxy, load balancer, and content caching server, and is often deployed in front of other web servers or application servers to handle incoming requests.

Why Use NGINX with Docker?

Combining Docker and NGINX can be a powerful solution for deploying and managing web applications. By running NGINX in a Docker container, you can easily scale, manage, and deploy your web application across different environments, while taking advantage of NGINX's performance and flexibility.

graph LR A[Client] --> B[NGINX Container] B --> C[Application Container] C --> D[Database Container]

Installing Docker and NGINX

To get started, you'll need to have Docker installed on your system. You can install Docker on Ubuntu 22.04 using the following commands:

sudo apt-get update
sudo apt-get install -y docker.io

Once Docker is installed, you can pull the NGINX Docker image from the Docker Hub registry:

docker pull nginx

Now you're ready to start configuring your NGINX container and setting up port binding.

Configuring NGINX Container Port Binding

To configure port binding for an NGINX container, you can use the -p or --publish flag when running the Docker container. This flag allows you to map a port on the host system to a port inside the container.

Running an NGINX Container with Port Binding

To run an NGINX container and bind it to port 8080 on the host system, you can use the following command:

docker run -d -p 8080:80 --name my-nginx nginx

In this command:

  • -d: runs the container in detached mode, which means it runs in the background.
  • -p 8080:80: maps port 8080 on the host system to port 80 inside the container.
  • --name my-nginx: assigns the name "my-nginx" to the container.
  • nginx: specifies the NGINX Docker image to use.

Verifying the Port Binding

You can verify that the port binding is working correctly by checking the running containers and accessing the NGINX web server from your web browser.

To list the running containers, use the following command:

docker ps

This should display the NGINX container you just created, with the port mapping information.

To access the NGINX web server, open a web browser and navigate to http://localhost:8080. You should see the default NGINX welcome page.

graph LR A[Host System] -- Port 8080 --> B[NGINX Container] B -- Port 80 --> C[NGINX Web Server]

By configuring the port binding, you can now access the NGINX web server running inside the Docker container from the host system.

Verifying the NGINX Container Setup

To verify that the NGINX container is set up correctly, you can perform the following steps:

Check the Running Containers

First, you can list the running containers on your system using the docker ps command:

docker ps

This should display the NGINX container you created earlier, along with the port mapping information.

Access the NGINX Web Server

Next, you can access the NGINX web server from your web browser by navigating to http://localhost:8080. You should see the default NGINX welcome page.

Check the NGINX Logs

You can also check the logs of the NGINX container to ensure that it is running without any issues. Use the following command to view the logs:

docker logs my-nginx

This should display the NGINX server logs, which you can use to troubleshoot any problems.

Inspect the Container

If you need more detailed information about the NGINX container, you can use the docker inspect command:

docker inspect my-nginx

This will provide a JSON-formatted output with various details about the container, such as its configuration, network settings, and resource usage.

By following these steps, you can verify that the NGINX container is set up correctly and is accessible from the host system.

graph LR A[Host System] -- Access NGINX --> B[NGINX Container] B -- Check Logs --> C[NGINX Logs] B -- Inspect Container --> D[Container Details]

Summary

This tutorial has provided a comprehensive guide on how to configure port binding for an NGINX container on port 8080 using Docker. You have learned the steps to set up the NGINX container and verify its successful deployment. With this knowledge, you can now confidently manage and deploy NGINX containers in your Docker-based infrastructure.

Other Docker Tutorials you may like