Node.js HTTP Get stream freezes inside docker container
Asked Answered
S

1

7

I have following code written in nodejs using http module. It's basically listens to event stream (so the connection is permanent).

http.get(EVENT_STREAM_ADDRESS, res => {
  res.on('data', (buf) => {
    const str = Buffer.from(buf).toString();
    console.log(str);
  });

  res.on('error', err => console.log("err: ", err));
});

If I run the above code on my Mac it works fine and I get data logged to console after many hours. But in the Docker, which has pretty basic configuration it stops receiving data after some time without any error. Other endpoints in the app are working fine (eg. Express endpoints) but this one http.get listener is just hanging.

Dockerfile


FROM node:current-alpine

WORKDIR /app

EXPOSE 4000

CMD npm install && npm run start

Do you have any ideas how I can overcome this?

It's really hard to debug as to reproduce the situation I sometimes need to wait a few hours.

Cheers

Scruggs answered 12/11, 2021 at 12:19 Comment(2)
Have you checked the container logs? Maybe the host computer sleeps and pause the Docker containers?Jandy
could be an issue related to timeout / memory, try putting a console log outside res.onAriminum
S
0

I find out what is probably happening.

The problem is Docker Desktop for Mac. It seems it stops container sometimes like in the situation that you Mac goes to sleep. And it interrupts the listener so it stops receiving new data chunks. I started same container on Ubuntu in VirtualBox and it works fine all the time.

Scruggs answered 7/12, 2021 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.