我正在通過React.js的教程,並且我決定在Docker容器中工作。 Here是本教程的代碼。無法連接到主機的(顯示,發佈)端口
本教程使用在容器上的端口8080上運行的webpack-dev-server
。我在我的Dockerfile
中公開了這個端口,並且我用-P
運行了我的容器來發布端口。在容器內部,如果我wget http://localhost:8080
,它按預期下載index.html
。但是,如果我從主機運行等效的PowerShell命令Invoke-WebRequest http://localhost:32769
,則會得到Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly.
以下全部詳細信息/重新生成步驟。
Dockerfile
FROM node
MAINTAINER Matthew Pirocchi <[email protected]>
RUN apt-get update && apt-get install -y vim
EXPOSE 8080
集裝箱設置
docker run -itdP --name repro mpiroc/react-fundamentals
docker exec -it repro bash
# Following commands executed within container
mkdir -p /home/mpiroc/repos && cd /home/mpiroc/repos
git clone https://github.com/ReactjsProgram/React-Fundamentals.git
cd React-Fundamentals
git checkout video2
npm install
npm run start # start just runs `webpack-dev-server`
測試容器
# In a separate terminal
PS C:\Users\matth> docker exec -it repro bash
[email protected]:/# wget http://localhost:8080
... # index.html is downloaded as expected
[email protected]:/# exit
exit
PS C:\Users\matth> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd6c3102bc51 mpiroc/react-fundamentals "node" 28 minutes ago Up 28 minutes 0.0.0.0:32769->8080/tcp repro
PS C:\Users\matth> Invoke-WebRequest http://localhost:32769
Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly.
At line:1 char:1
+ Invoke-WebRequest http://localhost:32769
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
爲什麼我無法從我的主機訪問發佈的端口?
這是非常有意義的,如果它只在容器中監聽本地主機,該端口對於容器外的端口轉發將不可見。 – BMitch