[英]Cannot connect to exposed port of container started with docker-compose on Windows
I'm having trouble accessing apps that should expose ports to the host via docker-compose.我在访问应该通过 docker-compose 向主机公开端口的应用程序时遇到问题。 Here is a reproducible example:这是一个可重现的示例:
I create a new angular app using the angular CLI:我使用 angular CLI 创建了一个新的 angular 应用程序:
ng new angular-docker
Then I create a Dockerfile with the following contents in that directory:然后我在该目录中创建一个包含以下内容的 Dockerfile:
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:接下来我在同一目录中创建一个 docker-compose.yaml 文件:
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我等到我在 docker-compose 输出中得到以下行
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:从docker inspect angular-docker
我看到端口转发规则已经到位:
...
"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.现在,当我尝试在 Chrome 中访问http://localhost:4200时,我得到 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
.但是,当我使用docker exec -it angular-docker bash
猛击容器时,当我curl localhost:4200
时,我得到了响应。
My environment:我的环境:
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"
netstat -an | findstr ":4200"
returns the following: netstat -an | findstr ":4200"
返回以下内容:
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.您不能在 docker 容器中使用默认的npm start
开箱即用。
One alternative is to update that command in your package.json
to run ng serve -H 0.0.0.0
like this:一种替代方法是在package.json
更新该命令以运行ng serve -H 0.0.0.0
如下所示:
"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.这个额外的-H 0.0.0.0
是用来监听来自容器的所有接口。
Then as your port mappings are working, you should get your site on the desired port localhost:4200
from the host machine.然后,当您的端口映射工作时,您应该从主机获取您的站点所需的端口localhost:4200
。
Would you please check http://127.0.0.1:4200请检查http://127.0.0.1:4200
On a lot of Linux servers I bump into the problem that "localhost" resolves into "::1" being ipv6 while the application is only configured to listen on ipv4 addresses.在许多 Linux 服务器上,我遇到了“localhost”解析为“::1”为 ipv6 而应用程序仅配置为侦听 ipv4 地址的问题。 Double-checking with "127.0.0.1" can figure that out.用“127.0.0.1”仔细检查可以解决这个问题。
When you use EXPOSE in docker, the port stay available to tcp connections wit others containers in your docker network.当您在 docker 中使用 EXPOSE 时,该端口对与 docker 网络中的其他容器的 tcp 连接保持可用。 If you want to be able to connect a port using http, you should map ports from your local machine to a container port.如果您希望能够使用 http 连接端口,您应该将端口从本地机器映射到容器端口。
PORTS
-3000:3000
Where the first port is the port on your local machine an the second ṕort is on the container.第一个端口是本地机器上的端口,第二个 ṕort 是容器上的端口。
Try to change like this尝试像这样改变
services:
application:
container_name: angular-docker
image: Your_image_build_from_docker_file
ports:
- "4200:4200"
volumes:
- ./src:/usr/src/app/src
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.