我正在通过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