Windows 如何访问本地主机API';docker集装箱内有什么?

Windows 如何访问本地主机API';docker集装箱内有什么?,windows,docker,Windows,Docker,我们有两个应用程序在系统中运行,一个来自docker,另一个直接在系统上运行,我们需要使用localhost:8082API访问容器应用程序中的系统应用程序 我们正在运行一个节点JS容器,该容器位于Windows 10主页上的端口5000,使用Docker toolbox,在容器外运行一个应用程序意味着直接从机器8042上运行,我们需要访问节点容器内的这个本地主机:8042API,我们如何存档它?,使用Docker版本19.03.1 运行cmd: docker container run -p

我们有两个应用程序在系统中运行,一个来自docker,另一个直接在系统上运行,我们需要使用
localhost:8082
API访问容器应用程序中的系统应用程序

我们正在运行一个
节点JS
容器,该容器位于
Windows 10
主页上的端口5000,使用
Docker toolbox
,在容器外运行一个应用程序意味着直接从机器8042上运行,我们需要访问节点容器内的这个
本地主机:8042
API,我们如何存档它?,使用
Docker版本19.03.1

运行cmd:

docker container run -p --net=host -it -v `pwd`:/usr/src/app --rm --name server server/windows:v1
错误:运行docker容器时连接被拒绝127.0.0.1:8042

docker container run --net=host -it -v `pwd`:/usr/src/app --rm --name server server/windows:v1
完全错误:

(node:80) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:8042
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
(node:80) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,
use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:80) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit code.
Dockerfile

FROM node:12

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install
COPY . .

# Specify port app runs on
EXPOSE 5000

# Run the app
CMD [ "npm", "run", "dev"]
错误:运行docker容器时连接拒绝127.0.0.1:8042

docker container run --net=host -it -v `pwd`:/usr/src/app --rm --name server server/windows:v1
这是因为主机上正在使用端口8042,因此在运行容器时无法映射该端口

docker container run --net=host -it -v `pwd`:/usr/src/app --rm --name server server/windows:v1
相反,您应该在运行容器时尝试
--net=host

docker container run --net=host -it -v `pwd`:/usr/src/app --rm --name server server/windows:v1

有关docker的
--net=host
属性的更多详细信息,请阅读。

获得相同的错误没有更改。无法从容器中访问外部主机,请执行以下操作:
docker run--add host=“localhost:PRIVATE\u host\u IP”-it-v`pwd`:/usr/src/app--rm name server/windows:v1
。私有主机IP可以使用命令
hostname-I | awk'{print$1}
找到,我使用的是Windows,主机名是ubuntu的,对吗?这个私有主机IP是容器IP?你需要找到一些可以访问的主机系统IP地址localhost通常是“此容器”。这在Docker Toolbox中更为棘手(因为它在VM中运行),但链接的问题已经存在。@DavidMaze,它工作正常,但我需要一个通用的解决方案,不仅仅是Docker Toolbox。因为很难在多个平台上使用它。我不能对所有平台使用相同的静态IP,对吗?