JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Exposing multiple ports in a Docker container

Author:JIYIK Last Updated:2025/03/24 Views:

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
  1. 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.
  2. 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.

Testing the Ports - Localhost 3000

Testing the Ports - Localhost 5000


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.

Previous:Using Docker network host commands

Next: None

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.

Article URL:https://www.jiyik.com/en/xwzj/opersys_10056.html

Related Articles

Using Docker network host commands

Publish Date:2025/03/24 Views:51 Category:Docker

Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking. When we create containers without specifying a network, they ar

Clear Docker container logs

Publish Date:2025/03/24 Views:192 Category:Docker

Logs are information recorded by the application when a specific event or state occurs. They help us monitor the application and take necessary actions. For example, when we deploy an application to a production environment, logs can help u

Docker daemon log location

Publish Date:2025/03/24 Views:65 Category:Docker

The Docker daemon provides essential information about the general state of your microservices architecture. Unfortunately, container-centric logging techniques allow you to collect relevant data from your services but provide little inform

Difference between COPY and ADD commands in Dockerfile

Publish Date:2025/03/24 Views:140 Category:Docker

A Dockerfile is a text document that contains all the commands used to build a Docker image. Recently, we have seen Docker being widely used as the default tool for managing configurations and automating deployments. Advanced features such

The --rm flag in Docker

Publish Date:2025/03/24 Views:151 Category:Docker

Typically, when most developers start using docker, after going through all the processes of pulling images, building them, and running containers, removing a container defeats the purpose of doing so. However, for experienced developers, t

Setting environment variables in Docker

Publish Date:2025/03/24 Views:188 Category:Docker

Environment variables are used to add additional configuration or metadata to aid in the development of an application, and can exist in different forms. For example, when developing a Java application, we usually set an environment variabl

在 Linux 中托管 Docker Internal

Publish Date:2023/04/18 Views:221 Category:Docker

Docker 允许开发人员通过将应用程序包装在称为容器的标准化单元中来高效地构建、测试和部署应用程序。 在使用 Docker 容器时,您可能会遇到需要将容器与主机连接的场景。

在 Docker 中设置工作目录

Publish Date:2023/04/18 Views:234 Category:Docker

在 Docker 中,我们可以通过编辑 Dockerfile 并添加密钥 WORKDIR 来设置我们的工作目录。本文将讨论在 Docker 中更改我们当前和默认的工作目录。

在 Docker 容器中公开多个端口

Publish Date:2023/04/18 Views:466 Category:Docker

Docker 容器使用端口来实现万维网上不同设备之间的通信。 在本篇文章中,我们将学习如何使用 Nginx 应用程序在 Docker 容器中公开多个端口。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial