Prasanna Prasanna - 2 months ago 12
Node.js Question

Dockerized Node js app does not start

After dockerizing my demo Express js app and starting the container, I am unable to access the service due to a

"Connection Timeout"


Url for the for project before dockerizing (Which produced "Hello world!" on the browser):

http://localhost:3000/cars/example/fetchResult


Url for the project after starting the docker container (Gives a "172.17.0.2 took too long to respond.")

http://172.17.0.2:3000/cars/example/fetchResult


Dockerfile

FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 3000
CMD [ "node", "server.js" ]


I built my docker image like

docker build -t prasannarb/example-node-service


I started my docker image as a container like

docker run -t prasannarb/example-node-service


Then when I,
docker ps
, it gives me

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7cf955f5d799 prasannarb/example-node-service "node server.js" About a minute ago Up About a minute 3000/tcp thirsty_perlman


docker inspect 7cf955f5d799
gives me
"IPAddress": "172.17.0.2"


Since I did not explicitly give a port to start my container, I was assuming it would take the same as exposed by my docker container (3000) which is the same port where my service would listen too.

What am I doing wrongly here?

Answer

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host.

To do that, you must use either the -p flag

Your docker run command should look like this:

$docker run -p3000:3000 -t prasannarb/example-node-service

Additionally, the docker inspect command gave you the container IP address, not the host IP address.