0

I have a javascript file (client.js) that runs node-fetch to make GET requests to my server. My server is listening on port 3000. Whenever I create a Docker container to run the client file, I get the following error:

(node:9) UnhandledPromiseRejectionWarning: FetchError: request to http://localhost:3000/selectItem/ failed, reason: connect ECONNREFUSED 127.0.0.1:3000

When I run the client file without any container, it works fine. I just want to have multiple container instances that run the client.js file.

I tried solutions like Node.js Error: connect ECONNREFUSED, but they didn't work.

My Dockerfile:

FROM node:10

WORKDIR /client

COPY package*.json ./

RUN npm install

COPY . .

CMD node client
EXPOSE 3000

Thank you :)

st.ov
  • 35
  • 1
  • 7
  • 2
    localhost inside the container is the container not the host system – Lawrence Cherone Jan 23 '21 at 16:01
  • Where is the server running? (Directly on the host, in another container?) The client? (In a browser, in a host Node setup, in a container?) What commands or actions produce that error message? Can you connect to the server in other ways? – David Maze Jan 23 '21 at 17:12
  • @DavidMaze The server is running locally (it is not inside a container). I only have one container and it is running the client.js script (executes web requests). It is possible to connect to the server if the client.js script is not inside a container. The command that produces the error is: docker run -it -p 8000:3000 client – st.ov Jan 23 '21 at 17:23
  • 1
    As @LawrenceCherone says, `localhost` is the container itself and not the host system. The linked question describes several workarounds. If you're using Docker Desktop on a Windows or MacOS system, the easiest is to use the special hostname `host.docker.internal` instead. – David Maze Jan 23 '21 at 17:39

0 Answers0