docker “localhost didn't send data. ERR_EMPTY_RESPONSE”和“curl:(52)来自服务器的空答复”

mbzjlibv  于 2023-04-05  发布在  Docker
关注(0)|答案(3)|浏览(161)

当我尝试访问Docker容器外部时,我得到了空数据,并出现了下面提到的错误。我在Docker容器内使用curl和wget命令没有任何问题。容器基于Angular JS镜像。
“localhost didn't send data. ERR_EMPTY_RESPONSE”和“curl:(52)来自服务器的空答复”
请帮帮我。
对不起!忘记发布实际代码。这里是我的Dockerfile和package.json文件。

包.json

{
  "name": "angularjs-project”,
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve -H 0.0.0.0",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.3.6",
    "@angular/common": "^4.2.4",
    "@angular/compiler": "^4.2.4",
    "@angular/core": "^4.2.4",
    "@angular/forms": "^4.2.4",
    "@angular/http": "^4.2.4",
    "@angular/platform-browser": "^4.2.4",
    "@angular/platform-browser-dynamic": "^4.2.4",
    "@angular/router": "^4.2.4",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "primeng": "^4.1.3",
    "rxjs": "^5.4.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.3.2",
    "@angular/compiler-cli": "^4.2.4",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.1.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

停靠文件

FROM node:6
RUN mkdir -p /angularjs-sample
ADD . /angularjs-sample
WORKDIR /angularjs-sample
RUN cd /angularjs-sample

RUN npm cache clean
RUN npm install -g @angular/cli
RUN npm install
RUN npm update
EXPOSE 4200
CMD ["ng", "serve"]
z0qdvdin

z0qdvdin1#

CMD ["ng", "serve", "--host=0.0.0.0"]

当HTTP客户端(如Web浏览器(或在本例中为curl))发送HTTP请求时,服务器需要客户端的IP地址,以便知道将响应发送到何处。当您运行本地开发服务器时,它会检查该IP地址,甚至不会响应不是来自www.example.com的请求127.0.0.1(localhost)。这可以防止网络上的其他人(比如,坐在同一家星巴克里)使用开发服务器作为入侵你的机器的一种方式。
当您在访客容器或VM中运行开发服务器,并在主机上运行客户端(curl)时,服务器会看到不同的IP地址,并拒绝响应。大多数服务器允许您指定有效客户端IP地址的掩码。0.0.0.0意味着fugetaboutit,接受所有客户端IP。它让保镖晚上休息。

vfwfrxfs

vfwfrxfs2#

我建议的理想方法是在创建时将主机IP地址绑定到容器。您不应该从Docker文件启动应用程序的运行。创建容器,与主机绑定并编写脚本来 Boot 应用程序
你可以这样做,

docker run -d -p 4545:8553--name my-container my-image my-command

其中“8553”可以是您的应用程序默认内部端口,与“4545”外部端口合并

hk8txs48

hk8txs483#

如果您的版本不推荐使用**--host**参数,请尝试此操作

const port = process.env.PORT || 3000;
  app.listen(port, "0.0.0.0", () => {
    console.log(
      `🚀 Application is running on: http://localhost:${port} env: ${process.env.NODE_ENV}`
    );
  });

相关问题