无法连接到在 Windows 上使用 docker-compose 启动的容器的暴露端口 [英] Cannot connect to exposed port of container started with docker-compose on Windows
问题描述
我在访问应通过 docker-compose 向主机公开端口的应用程序时遇到问题.这是一个可重现的示例:
我使用 angular CLI 创建了一个新的 angular 应用程序:
ng 新的 angular-docker
然后我在该目录中创建一个包含以下内容的 Dockerfile:
来自节点:8运行 mkdir -p/usr/src/app工作目录/usr/src/app复制 ./usr/src/应用程序运行 npm 安装曝光 4200CMD ["npm", "开始"]
接下来我在同一个目录下创建一个 docker-compose.yaml 文件:
版本:'3'服务:角度:建造: .容器名称:角度泊坞窗卷:- ./src:/usr/src/app/src端口:- 4200:4200"
然后我跑:
docker-compose up
我等到在 docker-compose 输出中得到以下行
angular-docker |** Angular Live Development Server 正在侦听 localhost: 4200,在 http://localhost:4200/上打开浏览器 **
从 docker inspect angular-docker
我看到端口转发规则已经到位:
<代码>...端口":{4200/tcp":[{"HostIp": "0.0.0.0",主机端口":4200"}]},...
现在,当我尝试在 Chrome 中访问 http://localhost:4200 时,我得到 ERR_EMPTY_RESPONSE.
但是,当我使用 docker exec -it angular-docker bash
猛击容器时,当我 curl localhost:4200
时,我得到了响应.>
我的环境:
- 操作系统:Windows 10 专业版 10.0.16299 内部版本 16299
- Docker:18.03.1-ce-win65 (17513)
我认为这可能是防火墙问题.因此,为了调试,我关闭了防火墙应用程序(我使用的是 Kaspersky Internet Security),但不幸的是.
为什么我不能从暴露的端口访问容器?
netstat -an |findstr ":4200"
返回以下内容:
Proto Local Address Foreign Address StateTCP 0.0.0.0:4200 0.0.0.0:0 监听TCP [::1]:4200 [::]:0 监听
你不能在 docker 容器中使用默认的 npm start
.
另一种方法是在您的 package.json
中更新该命令以运行 ng serve -H 0.0.0.0
像这样:
"start": "ng serve -H 0.0.0.0"
这个额外的-H 0.0.0.0
是用来监听容器中的所有接口.
然后,当您的端口映射正常工作时,您应该将您的站点从主机获取到所需的端口 localhost:4200
.
I'm having trouble accessing apps that should expose ports to the host via docker-compose. Here is a reproducible example:
I create a new angular app using the angular CLI:
ng new angular-docker
Then I create a Dockerfile with the following contents in that directory:
FROM node:8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
EXPOSE 4200
CMD ["npm", "start"]
Next I create a docker-compose.yaml file in the same directory:
version: '3'
services:
angular:
build: .
container_name: angular-docker
volumes:
- ./src:/usr/src/app/src
ports:
- "4200:4200"
Then I run:
docker-compose up
I wait until I get the following line in the docker-compose output
angular-docker | ** Angular Live Development Server is listening on localhost: 4200, open your browser on http://localhost:4200/ **
From docker inspect angular-docker
I see that the port-forwarding rule is in place:
...
"Ports": {
"4200/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "4200"
}
]
},
...
Now, when I try to go to http://localhost:4200 in Chrome, I get ERR_EMPTY_RESPONSE.
However, when I use docker exec -it angular-docker bash
to bash into the container, I get a response, when I curl localhost:4200
.
My environment:
- OS: Windows 10 Pro Version 10.0.16299 Build 16299
- Docker: 18.03.1-ce-win65 (17513)
I figured that this might be a firewall issue. So for debugging, I closed the firewall application (I'm using Kaspersky Internet Security), with no luck.
Why can I not access the container from the exposed port?
EDIT:
netstat -an | findstr ":4200"
returns the following:
Proto Local Address Foreign Address State
TCP 0.0.0.0:4200 0.0.0.0:0 LISTENING
TCP [::1]:4200 [::]:0 LISTENING
You can't use the default npm start
out of the box within a docker container.
One alternative is to update that command in your package.json
to run ng serve -H 0.0.0.0
like this:
"start": "ng serve -H 0.0.0.0"
This extra -H 0.0.0.0
is to listen to all the interfaces from the container.
Then as your port mappings are working, you should get your site on the desired port localhost:4200
from the host machine.
这篇关于无法连接到在 Windows 上使用 docker-compose 启动的容器的暴露端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!