TypeScript Docker Container not working

I have written a server in typescript and trying to run on local using a docker container.
The Docker container builds successfully and starts as well.
But when I try to hit the API endpoint for checking health-status, postman returns me a socket hang up response and the web returns me an empty response.

If I try without the container and directly run it, the server endpoints work.

Based on recent topics, people get empty response when they forward a port from the host to a wrong port in the container. but without knowing how your healthcheck works and from where you want to access the webpage, it is hard to tell you more ideas.

Hi
I was also having the same doubt but I run the container with the following command:-
docker run -p 8080:8080 <container_name>

In the dockerfile also I have exposed port 8080 only.

It doesn’t matter what you expose in a Dockerfile as that is just metadata and doesn’t forward or allow anythig directly. If the application doesn’t listen on port 8080 in the container, it also doesn’t matter that you forwarded a port from the host to port 8080 in the container.

So the following is the dockerfile code:-
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npx tsc
EXPOSE 8080
ENV NODE_ENV=qa.env
CMD [“npm”, “run”, “qa”]
With this dockerfile code, I am able to build a container. If I try to hit my server endpoint from the exec console of the docker desktop, I am able to hit it successfully. But if I try to hit it with my cmd, I get “no reply from server” error.

The commands that I use on docker cli are:-

docker build -t ts_server .
docker run -p 8080:8080 ts_server

I have already configured the server to listen on port 8080.
Hope this information helps you in understanding the situation.

Then the application is probbaly listening only on localhost. You need to configure it to listen on 0.0.0.0. Which means “on all available ip addresses in the container”.

I tried what you said…
I changed the dockerfile’s last line to listen on 0.0.0.0

CMD [“npm”, “run”, “qa”, “–”, “–host”, “0.0.0.0”]

But still the same issue persists. I am not able to hit from outer cmd or postman.

I don’t know if that setting is correct. I can only ask a couple of quetions.

  1. What command did you use in the container to check that the port worked inside?
  2. Where and how did you configure the app to listen on port 8080?
  3. Have you tried curl from the container with the container IP instead of localhost??
  4. Have you tried the docker run command with a different host port like
    docker run -p 9999:8080 ts_server
    
    and then accessing it from a webbrowser on port 9999?

Also please check this guide to format your share codes with code blocks instead of quotes: How to format your forum posts

  1. So I tried curl from Exec inbuilt console on the docker desktop and was able to get status 200. But as soon as tried curl from terminal, I got an empty reply from server.

  2. in the env file I configured the server to listen on port 8080. I also exposed port 8080 in the dockerfile as well.

  3. I don’t know how to find the container ip so can try this approach.

  4. yes tried this as well but no success.

Again, exposing ports does nothing. The fact that you have an environment file does not get us closer to how you set the port. Can you show the relevant part in the nodejs code?

IS that file read by the application or by Docker? You should try to set the port statically in the code without using environment variables just fo testing. If that works, the variable was not read c orrectly.

Well, you can search for “docker container ip” on Google and have many wys to find the ip address. In short, docker inspect containername can show you the ip and also ip addr executed in the container but that depends on whether iprouter2 is installed in your container.

Are you sure the process inside the container really listens on port 8080? You can verify if it does, by opening a terminal in the running container and try to access application from inside

# if curl exists in the image:
docker exec -ti {container name or id} curl http://localhost:8080

# if wget exists in the image:
docker exec -ti {container name or id} wget -O - http://localhost:8080

Note: of course, you need to replace {container name or id} with the actual value.

Either one of the commands should return the html response of the nodejs application if it really listens on that port.

Furthermore, It’s not a good idea start writing an application from scratch with an outdated and unsupported version of nodejs. It has unfixed vulnerabilities, and you will start of with technical debt of having to migrate to a supported and maintained version immediately.

I strongly suggest to use nodejs 20 instead of 14. You might want to take a look at the release schedule, to see which version is supported/maintained. Nodejs 14 is not even listed anymore…

See GitHub - nodejs/Release: Node.js Release Working Group for the release schedule.

IS that file read by the application or by Docker? You should try to set the port statically in the code without using environment variables just fo testing. If that works, the variable was not read c orrectly.

The ENV file contains the variables for AWS and DB and is used by the server files for multiple performing operations.

I don’t understand your answer as I asked about what reads the environment file and you mentioned what the variables are for.

It looks like I’m not doing a very good job in asking for information. The point is that if you don’t share source code or anything that shows the code should do and you don’t clarify how that environment file is used, we can’t help you.

I think we shared all you need to know about accessing ports from outside a container. You are basically saying you are doing everything correctly, so the only next step could be that you show us what you are doing, Commands, compose files if you have any and the relevant part of the source code. Otherwise it is a black box for us and there is nothing more we can tell you.

Hi @rimelek
I was able to finally run the project through the docker container.
Thanks for all the help that you provided. My port mapping was correct but I had the server configured to run only on localhost. So as you asked I configured it for 0.0.0.0 and I was able to successfully hit all the responses.

Sorry was not able to share the source code or env file much beacuse of confidentiality issues but I appreciated the help that you provided without that. :slight_smile: