skip to Main Content

I have NodeJS/TypeScript application (github repo) which is working fine when I run the script defined in package.json. i.e., npm run start will start my local host and I can hit endpoint via POSTMAN.

I have created docker image (I am new to Docker and this is my first image). Here, I am getting Error: connect ECONNREFUSED 127.0.0.1:7001 error in POSTMAN.

I noticed that I do not see Listening on port 7001 message in terminal when I run docker file. This tells me that I am making some mistake in .Dockerfile.

Steps:

  • I created docker image using docker build -t <IMAGE-NAME> . I can see successfully created image.
  • I launched container using docker run --name <CONTAINER-NAME> <IMAGE-NAME>
  • I’ve also disabled Use the system proxy setting in POSTMAN but no luck.

Details:

  • Package.json file
"scripts": {
    "dev": "ts-node-dev --respawn --pretty --transpile-only src/server.ts",
    "compile": "tsc -p .",
    "start": "npm run compile && npm run dev"
  }
  • Response from terminal when I run npm run start (This is successful)

enter image description here

  • Dockerfile
#FROM is the base image for which we will run our application
FROM node:12.0.0

# Copy source code
COPY . /app 

# Change working directory
WORKDIR /app

# Install dependencies
RUN npm install
RUN npm install -g typescript

# Expose API port to the outside 
EXPOSE 7001

# Launch application
CMD ["npm", "start"]
  • Response after running docker command

enter image description here

  • GitHub repo structure

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Finally, I was able to make this work with two things:

    1. Using @Dante's suggestion (mentioned above).
    2. Updating my .Dockerfile with following:
    FROM node:12.0.0
    
    # Change working directory
    WORKDIR /user/app
    
    # Copy package.json into the container at /app
    COPY package*.json ./
    
    # Install dependencies
    RUN npm install
    RUN npm install -g typescript
    
    # Copy the current directory contents into the container at root level (in this case, /app directory)
    COPY . ./
    
    # Expose API port to the outside 
    EXPOSE 7001
    
    # Launch application
    CMD ["npm", "run", "start"]
    

  2. By any chance did you forget to map your container port to the host one?

    docker run --name <CONTAINER-NAME> -p 7001:7001 <IMAGE-NAME>

    the -p does the trick of exposing the port to your network. The number on the left side is the container port (7001 as exposed on the Dockerfile) and the second one is the target port on the host machine. You can set this up to other available ports as well. Eg.: -p 7001:3000to expose on http://localhost:3000

    Check out Docker documentation about networking

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search