Exposing multiple ports in a Docker container
There are different types of communication on the Internet, the most common ones include file transfers, sending emails, and serving web pages. To make this communication possible, we make use of port numbers that help identify the type of communication.
For example, File Transfer Protocol uses ports 20 and 21, Simple Mail Transfer Protocol uses 25, and Hypertext Transfer Protocol uses 25.
Similarly, Docker containers use ports to enable communication between different devices on the World Wide Web. In this article, we will learn how to expose multiple ports in a Docker container using the Nginx application.
Creating an NGINX Application
Open WebStorm IDEA and select File > New > Project. In the window that opens, select Empty Project and change the project name from untitled to web-app.
Finally, press the button labeled Create to create an empty project.
请注意
, we can also use any other development environment as it does not matter what development environment you use. Since NGINX is used to serve static content, we do not need any additional configuration files.
After the project is built, create a file called index.js in the web-app folder and copy and paste the following code into the file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to Nginx !</h1>
</body>
</html>
This file contains a simple web page with a title that helps us test the application. Modify the HTML content to display whatever content you want.
Create a Dockerfile
Create a file called Dockerfile in the web-app folder and copy and paste the following instructions into the file.
FROM nginx:1.23.1-alpine
ADD . /usr/share/nginx/html
- FROM - sets the base image on which to create the custom image, in our case we use alpine to extract a lightweight version of NGINX.
- ADD - Copies the files and folders in the current folder to our mirrored file system /usr/share/nginx/html.
Building an Image
Use the keyboard shortcut ALT+F12 to open a new terminal window in your development environment and use the following command to create an image with the tag web-app:latest.
~/WebstormProjects/web-app$ docker build --tag web-app:latest .
This command executes our Dockerfile. We can view the two instructions executed in sequence as follows.
=> [1/2] FROM docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c3740 0.2s
=> => resolve docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c3740 0.2s
=> CACHED [2/2] ADD . /usr/share/nginx/html
Running a Docker container with multiple ports
In the same terminal window that you used to build the image, use the following command to run a container named web-app-prod listening on port 80 on the container, with ports 3000 and 5000 on the host.
~/WebstormProjects/web-app$ docker run --name web-app-prod -d -p 3000:80 -p 5000:80 web-app:latest
To expose multiple ports on the container, we used two consecutive -p flags to allocate two different ports on the host to listen on port 80 on the container.
Test Port
To verify that our containers are working as expected, open any browser and make requests to localhost:3000 ( http://localhost:3000/ ) and localhost:5000 ( http://localhost:5000/ ). Since the ports are listening to the same container, we get the same page of the NGINX application returned for both requests, as shown below.
Summarize
This article taught us how to expose multiple ports in a Docker container using the NGINX application. Note that this is the most common way to expose multiple ports, but we can use other methods to achieve the same goal.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application