英文:
How to connect to localhost from a docker container?
问题
I have a Python application to expose a REST API. The Python server is running on http://127.0.0.1:5000
我有一个用于暴露 REST API 的 Python 应用程序。Python 服务器运行在 http://127.0.0.1:5000
I have another application written in NodeJS to wrap the API coming from Python and expose another API as a passthrough. The Node server is running on http://localhost:8080
我有另一个用 NodeJS 编写的应用程序,用于包装来自Python的API,并将另一个API公开为透传,Node 服务器运行在 http://localhost:8080
I'm new to Docker, I'm building a docker image for the Node application (in MacOS Silicon). The problem is when I invoke the API using curl http://localhost:8080
The docker desktop says "Connection refused: /127.0.0.1:5000"
我对Docker还不熟悉,我正在为Node应用程序构建一个Docker映像(在MacOS Silicon上)。问题是,当我使用 curl http://localhost:8080
调用API时,Docker桌面显示 "Connection refused: /127.0.0.1:5000"
The following is the docker run command I'm using
以下是我正在使用的docker运行命令
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0
I tried with the --network host flag with the docker run command, but it doesn't work since it ignores all the declared ports. And I tried with the http.server --bind 0.0.0.0 with the docker run command, but the result says "pull access denied for http.server"
我尝试使用docker运行命令的--network host标志,但它不起作用,因为它忽略了所有声明的端口。而且我尝试使用docker运行命令的http.server --bind 0.0.0.0,但结果显示 "pull access denied for http.server"
How can I solve this?
我该如何解决这个问题?
英文:
I have a Python application to expose a REST API. The Python server is running on http://127.0.0.1:5000
I have another application written in NodeJS to wrap the API coming from Python and expose another API as a passthrough. The Node server is running on http://localhost:8080
I'm new to Docker, I'm building a docker image for the Node application (in MacOS Silicon). The problem is when I invoke the API using curl http://localhost:8080
The docker desktop says "Connection refused: /127.0.0.1:5000"
The following is the docker run command I'm using
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0
I tried with the --network host
flag with the docker run
command, but it doesn't work since it ignores all the declared ports. And I tried with the http.server --bind 0.0.0.0
with the docker run
command, but the result says "pull access denied for http.server"
How can I solve this?
答案1
得分: 0
curl http://127.0.0.1:5000
在主机上运行,以查看服务器是否实际在运行。
从容器内检查服务器的可访问性。只需在交互式it
上运行Docker容器,然后尝试从容器中访问服务器。
docker run --platform linux/amd64 -it myApp/app:v0.1.0 /bin/bash
curl http://127.0.0.1:5000
如果这不起作用,那么容器中存在网络配置问题,您应该设置以允许访问主机机器。
如果可以正常工作,则将NodeJs应用绑定到0.0.0.0以允许从任何IP连接。
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0 -host 0.0.0.0
英文:
curl http://127.0.0.1:5000
on host machine to see if server is actually running.
Check server accessibility from within container. Simply run the docker container on interactive it
and try access the server from the container.
docker run --platform linux/amd64 -it myApp/app:v0.1.0 /bin/bash
curl http://127.0.0.1:5000
If this does not work, it's a network configuration issue in the container you should set up to allow access to host machine.
If it works, then bind NodeJs app to 0.0.0.0 to allow conns from any ip.
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0 -host 0.0.0.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论