如何从 Docker 容器连接到本地主机?

huangapple go评论46阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月18日 01:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定