要暴露Docker容器端口,需通過端口映射使主機(jī)可訪問容器服務(wù)。 1. 使用docker run -p [host_port]:[container_port]命令運(yùn)行容器,如docker run -p 8080:3000 my-web-app;2. Dockerfile中使用EXPOSE指令標(biāo)註用途,如EXPOSE 3000,但不會(huì)自動(dòng)發(fā)布端口;3. Docker Compose中在yml文件的ports段配置,如ports: - "8080:3000";4. 運(yùn)行後使用docker ps檢查端口映射是否生效。以上方法確保正確暴露並驗(yàn)證容器端口。
Exposing a port from a Docker container to the host machine is a common task when running services like web servers, APIs, or databases inside containers. The key is mapping the container's internal port to a port on your host system so you can access it from outside Docker.
1. Use the -p
Flag with docker run
The most straightforward way to expose a port is by using the -p
(or --publish
) flag when running a container with docker run
.
Basic syntax:
docker run -p [host_port]:[container_port] [image_name]
For example, if you're running a web app inside the container that listens on port 3000:
docker run -p 8080:3000 my-web-app
Now you can access the service via http://localhost:8080
on your host machine.
- You can specify multiple ports by repeating the
-p
flag. - If you omit the host port (
-p 3000
), Docker will assign a random available port automatically. - This works for TCP and UDP; use
-p 53:53/udp
to expose UDP ports specifically.
2. Define Ports in a Dockerfile with EXPOSE
You can document which ports your image uses by adding the EXPOSE
instruction in your Dockerfile:
EXPOSE 3000
However, this does not actually publish the port —you still need to use -p
when running the container. Think of EXPOSE
as metadata for whoever runs the container later.
If you're building an image others will use, including EXPOSE
helps them know what ports are relevant without guessing.
3. Use Docker Compose with the ports
Section
If you're using Docker Compose, define port mappings under the ports
section in your docker-compose.yml
file:
services: webapp: image: my-web-app ports: - "8080:3000"
This has the same effect as the -p
flag but keeps your configuration centralized and version-controlled.
- You can also use short syntax like
"3000"
to let Docker assign a random host port. - For more control, use long syntax with extra options like protocol or mode.
4. Check Open Ports with docker ps
After starting your container, verify the port mapping with:
docker ps
Look at the "PORTS" column. It should show something like:
0.0.0.0:8080->3000/tcp
That confirms your host's port 8080 is forwarding to port 3000 in the container.
If you don't see any port listed, double-check that you used -p
or configured ports
correctly.
And that's basically it. Exposing ports isn't complicated once you know how Docker maps them—but it's easy to overlook a small detail like forgetting the -p
flag or assuming EXPOSE
does more than it does.
以上是您如何將端口從Docker容器公開到主機(jī)機(jī)器?的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

熱門話題

退出 Docker 容器的四種方法:容器終端中使用 Ctrl D 快捷鍵容器終端中輸入 exit 命令宿主機(jī)終端中使用 docker stop <container_name> 命令宿主機(jī)終端中使用 docker kill <container_name> 命令(強(qiáng)制退出)

Docker 容器啟動(dòng)步驟:拉取容器鏡像:運(yùn)行 "docker pull [鏡像名稱]"。創(chuàng)建容器:使用 "docker create [選項(xiàng)] [鏡像名稱] [命令和參數(shù)]"。啟動(dòng)容器:執(zhí)行 "docker start [容器名稱或 ID]"。檢查容器狀態(tài):通過 "docker ps" 驗(yàn)證容器是否正在運(yùn)行。

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

Docker 中將文件拷貝到外部主機(jī)的方法:使用 docker cp 命令:執(zhí)行 docker cp [選項(xiàng)] <容器路徑> <主機(jī)路徑>。使用數(shù)據(jù)卷:在主機(jī)上創(chuàng)建目錄,在創(chuàng)建容器時(shí)使用 -v 參數(shù)掛載該目錄到容器內(nèi),實(shí)現(xiàn)文件雙向同步。

重啟 Docker 容器的方法:獲取容器 ID(docker ps);停止容器(docker stop <container_id>);啟動(dòng)容器(docker start <container_id>);驗(yàn)證重啟成功(docker ps)。其他方法:Docker Compose(docker-compose restart)或 Docker API(參考 Docker 文檔)。

在 Docker 中創(chuàng)建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創(chuàng)建容器: docker run [選項(xiàng)] [鏡像名] [命令] 3. 啟動(dòng)容器: docker start [容器名]

在 Docker 中啟動(dòng) MySQL 的過程包含以下步驟:拉取 MySQL 鏡像創(chuàng)建並啟動(dòng)容器,設(shè)置根用戶密碼並映射端口驗(yàn)證連接創(chuàng)建數(shù)據(jù)庫(kù)和用戶授予對(duì)數(shù)據(jù)庫(kù)的所有權(quán)限

查看 Docker 日誌的方法包括:使用 docker logs 命令,例如:docker logs CONTAINER_NAME使用 docker exec 命令運(yùn)行 /bin/sh 並查看日誌文件,例如:docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log使用 Docker Compose 的 docker-compose logs 命令,例如:docker-compose -f docker-com
