无法从主机访问运行中的Docker容器(localhost:8081)

使用Ubuntu。

基于本指南:

https://www.freecodecamp.org/news/how-to-use-routing-in-vue-js-to-create-a-better-user-experience-98d225bbcdd9/

我用以下项目结构创建了一个最小的vuejs项目:

https://github.com/dev-samples/samples/tree/master/vuejs-001

frontend-router/
  build/
  config/
  src/
  static/
  test/
  build.sh
  Dockerfile.dev
  package-lock.json
  package.json

位置:

Dockerfile.dev

FROM node:10
RUN apt install curl
RUN mkdir /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json

# make the 'app' folder the current working directory before running npm install
WORKDIR /app

RUN npm install
CMD [ "npm","run","dev" ]

我正在构建映像,并使用以下命令从该映像运行容器:

docker build -t frontend-router-image -f Dockerfile.dev .
docker rm -f frontend-router-container

docker run -it -p 8081:8080 -v ${PWD}:/app/ -v /app/node_modules --name frontend-router-container frontend-router-image

给出:

DONE  Compiled successfully in 1738ms                                                                                                                                                    3:49:45 PM

 I  Your application is running here: http://localhost:8080

自从我将-p 8081:8080添加到docker run命令之后,我希望可以从主机浏览器访问以下应用程序:

http://localhost:8081/

但是它只给出以下错误:

无法从主机访问运行中的Docker容器(localhost:8081)

当我从主机上使用香草npm运行它时,我可以正常工作。但是为什么从泊坞窗容器中运行应用程序时为什么不能访问该应用程序?

此处的源代码:

https://github.com/dev-samples/samples/tree/master/vuejs-001

如下所示,我已经尝试过:

$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                    NAMES
e011fb9e39e8        frontend-router-image   "docker-entrypoint.s…"   12 seconds ago      Up 9 seconds        0.0.0.0:8081->8080/tcp   frontend-router-container

$ docker run -it --rm --net container:frontend-router-container nicolaka/netshoot ss -lnt
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128              127.0.0.1:8080             0.0.0.0:*       

为了进行比较,该项目运行良好:

https://github.com/dev-samples/samples/tree/master/vuejs-002

这意味着当我运行容器时,可以在localhost:8081的主机浏览器上访问Web应用程序

bellebei 回答:无法从主机访问运行中的Docker容器(localhost:8081)

基于此:

https://github.com/webpack/webpack-dev-server/issues/547

和:

https://dev.to/azawakh/don-t-forget-to-give-host-0-0-0-0-to-the-startup-option-of-webpack-dev-server-using-docker-1483

https://pythonspeed.com/articles/docker-connection-refused/

如果我进行更改,它将起作用:

host: 'localhost',// can be overwritten by process.env.HOST

收件人:

host: '0.0.0.0',// can be overwritten by process.env.HOST

在文件中: /frontend-router/config/index.js

,

当您拥有connection reset时,通常意味着没有人在端口上监听。

似乎您在听localhost,您必须 在docker中时监听0.0.0.0。

在文件config/index.js中,hostlocalhost,您必须删除host指令

如果您在127.0.0.1localhost上收听,则说明您在本地网络上收听,因此 在容器内部,只能通过本地进程访问Web服务器。

您可能遇到的另一种问题,就是连接到错误的端口。

如果您使用docker run -it -p 8081:8080运行,则必须访问http://localhost:8081/

请参见

  

发布或公开端口(-p,-expose)   来自https://docs.docker.com/engine/reference/commandline/run/

本文链接:https://www.f2er.com/3156573.html

大家都在问