016 docker容器與宿主機的埠對映

2021-07-31 23:57:11 字數 1179 閱讀 2899

從之前構建的映象啟動乙個容器

# -p 80:docker容器公開給宿主機的網路埠,此時docker可以在宿主機上隨機選擇乙個位於32768~61000之間的埠號來對映到容器的80埠上

# nginx -g "daemon off;":指定容器中需要執行的指令,這條指令的含義是以前臺執行的方式啟動nginx

sudo docker run -d -p 80 --name static_web jamtur01/static_web nginx -g "dameon off;"

檢視容器的埠對映情況

# 方式1

sudo docker ps -l

# 方式2

sudo docker port static_web 80

指定到宿主機的指定埠

# 將容器中的80埠,繫結到宿主機的8080埠

sudo docker run -d -p 8080:80 --name static_web jamtur01/static_web nginx -g "dameon off;"

繫結埠時,可限制ip

# 範例1:將容器內的80埠繫結到宿主機的127.0.0.1這個ip的8080埠

sudo docker run -d -p 127.0.0.1:8080:80 --name static_web jamtur01/static_web nginx -g "dameon off;"

# 範例2:將容器內的80埠繫結到宿主機的127.0.0.1這個ip的隨機埠

sudo docker run -d -p 127.0.0.1::80 --name static_web jamtur01/static_web nginx -g "dameon off;"

-p的使用

# 對宿主機公開在dockerfile中通過expose指令公開的所有埠

sudo docker run -d -p --name static_web jamtur01/static_web nginx -g "dameon off;"

docker宿主機訪問docker容器服務失敗

原因 因為docker的虛擬ip網段是172.17.與區域網的ip網段172.17衝突了,所以有兩種方式 解決方法 一 修改docker網絡卡資訊,將網段改為與區域網不同的即可 linux修改方法 第一步 刪除原有配置 sudo service docker stop sudo ip link se...

Docker容器訪問宿主機網路

業務請求量小的時候,我們會把一些工程部署到同一臺機器上。這些工程之間也會相互訪問。如果是http的介面,我們最方便的是使用localhost帶地本機的ip。不過結合docker容器後出現了問題。docker容器中localhost表示容器的ip位址。不是宿主機ip。其實docker容器執行的時候有h...

docker 宿主機與容器間複製檔案

將當前目錄的test.txt檔案複製到container容器的 home目錄下 docker cp container home test.txt home 將container容器內檔案複製到宿主機 我使用的為centos7 docker 1.12.6,其他版本不確定是否提供該命令 示例 假設我有...