AlwaysALearner
AlwaysALearner

Reputation: 355

"localhost didn't send data. ERR_EMPTY_RESPONSE" and "curl: (52) Empty reply from server"

I am getting empty data with below mentioned error when I try to access outside of Docker container. I don't have any issue with both curl and wget commands inside the Docker container. The container is based on Angular JS image.

"localhost didn't send data. ERR_EMPTY_RESPONSE" and "curl: (52) Empty reply from server"

Please help.

Sorry! Forgot to post actual code. Here my Dockerfile and package.json files.

package.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"
  }
}

Dockerfile

    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"]

Upvotes: 7

Views: 11035

Answers (4)

Hamid
Hamid

Reputation: 897

Try the below code

FROM node:latest as builder

EXPOSE 4200

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install -g @angular/[email protected]

RUN npm ci --force

COPY . .

RUN npm run build:production

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

  1. For creating image => docker build -t myimage .

  2. For running image => docker run -p 32544:4200 myimage

Be carefull, if you face the same error again, change the 4200 to another random port! Also '32544' is just random port as well, you can set whatever you want!

docker run host-port:docker-port yourImageName

Upvotes: 1

mesutpiskin
mesutpiskin

Reputation: 1937

Try this if the --host parameter is deprecated for your version

 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}`
    );
  });

Upvotes: 0

Jeff Schwab
Jeff Schwab

Reputation: 609

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

When an HTTP client like a web browser (or in this case, curl) sends an HTTP request, the server needs the client’s IP address, so it knows where to send the response. When you run a local development server, it inspects that IP address, and won’t even respond to requests that aren’t coming from 127.0.0.1 (localhost). That keeps other folks on your network (say, sitting in the same Starbucks) from using the dev server as a way to hack your box.

When you run a dev server in a guest container or VM, and run the client (curl) on the host machine, the server sees a different IP address, and refuses to respond. Most servers let you specify a mask of what IP addresses are valid clients. 0.0.0.0 means fugetaboutit, accept all client IPs. It gives the bouncer the night off.

Upvotes: 11

Sohan
Sohan

Reputation: 6809

Ideal way to do this I would suggest is to bind the host machine ip address to your container when creating. You should not initiate running of your application from docker file. Create container, bind with your host machine and the write a script to boot your application

You can do this as ,

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

Where "8553" can be your application default internal port binned with "4545" external port

Upvotes: 1

Related Questions