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 runnpm run start
(This is successful)
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
- GitHub repo structure
2
Answers
Finally, I was able to make this work with two things:
.Dockerfile
with following: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:3000
to expose on http://localhost:3000Check out Docker documentation about networking