Vincenzo
Vincenzo

Reputation: 6358

Error: connect ECONNREFUSED 0.0.0.0:8000 when hitting Docker containerised Node.js app endpoint

I'm just starting with Docker and dough I succeed in creating an image and a container from it I'm not succeeding in connecting to the container's port with postman and I get Error: connect ECONNREFUSED 0.0.0.0:8000.

In my server.js file I have:

const app = require('./api/src/app');

const port = process.env.PORT || 3000; // PORT is set to 5000

app.listen(port, () => {
  console.log('App executing to port ', port);
});

in my index.js I have :

const express = require('express');

 const router = express.Router();
 
 router.get('/api', (req, res) => {
   res.status(200).send({
     success: 'true',
     message: 'Welcome to fixit',
     version: '1.0.0',
   });
 });
 
 module.exports = router;

so if I run my app with either npm start or nodemon server.js the localhost:3000/api endpoint works as expected.

I then build a docker image for my app with the command docker build . -t fixit-server with this Dockerfile:

FROM node:15.14.0

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 5000

# CMD ["npm", "start"]
CMD npm start
# CMD ["nodemon", "server.js"]

and run the container with the command docker run -d -p 8000:5000 --name fixit-container fixit-server tail -f /dev/null

and listing the containers with docker ps -a shows it running :

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                    NAMES
da0e4ef12402   fixit-server   "docker-entrypoint.s…"   9 seconds ago   Up 8 seconds   0.0.0.0:8000->5000/tcp   fixit-container

but when I hit the endpoint 0.0.0.0:8000/apiI get the ECONNREFUSED error. I tried both CMD ["npm", "start"]and CMD npm start but I get the error both ways. Can you what I'm doing wrong?

Upvotes: 0

Views: 2459

Answers (1)

Eranga Heshan
Eranga Heshan

Reputation: 5804

Update:

@Vincenzo was using docker-machine and to be able to check whether the app was working properly, we needed to execute the following command in the terminal:

docker-machine env

The result was:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.102:2376"
export DOCKER_CERT_PATH="/Users/vinnytwice/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"

Then based on the DOCKER_HOST value, we hit 192.168.99.102:8000/api and it was working.


I believe the problem is you're never setting the PORT environment variable to 5000.

EXPOSE docker command is a no op. Meaning that it will do nothing but is only for the developer to know that you're exposing the port 5000. You can read it in Docker documentation.

You need to either set an environment variable or pass an environment variable at runtime to the container to specifically tell it that PORT is 5000.

Method 1:

You can change your Dockerfile like below:

FROM node:15.14.0

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

ENV PORT=5000

EXPOSE $PORT

# CMD ["npm", "start"]
CMD npm start
# CMD ["nodemon", "server.js"]

Method 2:

Simply use the following command to run your container:

docker run -d -p 8000:5000 --name fixit-container --env PORT=5000 fixit-server

Upvotes: 3

Related Questions