Docker-Compose 客户端 API 对服务器的调用失败...默认网络服务别名不起作用?
我想知道如何解决这个问题!本质上,默认 Docker 网络上通过服务名称进行的动态路由不起作用。 :(
我曾考虑过让它使用静态 IP,但这可能比解决原始问题更多。
docker-compose.yml
version: "3.8"
services:
client:
build: client
ports: [3000]
depends_on:
- server
volumes:
- ./client:/app
mongodb:
image : mongo
environment:
- PUID=1000
- PGID=1000
ports: [27017]
restart: unless-stopped
volumes:
- ./mongodb/database:/data/db
server:
build: server
ports: [5000]
restart: always
depends_on:
- mongodb
volumes:
- ./server:/app
如果我从客户端进行 ping 测试,它将通过浏览器工作
http://0.0.0.0:49246/ping
但是,我真的希望它能够这里的工作方法
http://server:5000/ping
有帮助吗?
I'm wondering how to fix this problem! Essentially the dynamic routing via service name on the default Docker network is not working. :(
I had considered making it use a static IP but that may be more work than fixing the original issue.
docker-compose.yml
version: "3.8"
services:
client:
build: client
ports: [3000]
depends_on:
- server
volumes:
- ./client:/app
mongodb:
image : mongo
environment:
- PUID=1000
- PGID=1000
ports: [27017]
restart: unless-stopped
volumes:
- ./mongodb/database:/data/db
server:
build: server
ports: [5000]
restart: always
depends_on:
- mongodb
volumes:
- ./server:/app
If I do this ping test from the client it will work via the browser
http://0.0.0.0:49246/ping
However, I really want it to work in this approach here
http://server:5000/ping
Help? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Web应用程序代码在浏览器中运行,而不是托管文件的服务器。浏览器通常在主机上运行。
听起来您希望服务器映射主机上的已知端口。尝试使用它将主机端口5000映射到
服务器
容器端口5000 ...并在您的Web应用程序代码中连接
http:// localhost:5000
。参见 https://docs.docks.docks.docker.com/compose/撰写文件/撰写file-v3/#ports
如果您要Web应用程序来代理请求(例如,使用创建React App App
代理
配置),您将使用内部主机名,因为此操作由此操作执行客户端
服务。Web app code runs in the browser, not the server hosting the files. The browser is typically running on the host.
It sounds like you want the server to map a known port on the host. Try using this to map host port 5000 to the
server
container port 5000...and connect on
http://localhost:5000
in your web app code.See https://docs.docker.com/compose/compose-file/compose-file-v3/#ports
If you're wanting your web app to proxy requests (for example using the Create React App
proxy
configuration), you would use the internal host name since this action is performed by theclient
service.