Reputation: 2641
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?
Upvotes: 4
Views: 4776
Reputation: 28920
I encountered a similar issue when working on a Node JS app using Docker.
When I check the logs for the docker app using the command:
docker logs <container-id>
it shows that the app is connected and synced to the database:
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'tbl_user_skills' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname; Database Connected and Synced
But when I run the command:
curl localhost:3000
to check the app, I get the error below:
curl: (56) Recv failure: Connection reset by peer
I've checked the AWS security group for the server where the app is deployed and it's open for port 3000
.
Here's how I solved it:
The issue was that the port which I exposed in the Dockerfile is not the port that the app is listening to.
I did an exec into the docker container to check the logs using the command:
docker exec -it <container-id> /bin/sh
Next, I checked the logs files (error.log
and info.log
) which are located in the logs
folder:
# cd logs
# cat error.log
# ls
error.log info.log warnings.log
# cat info.log
info: Apr-03-2022 09:08:41: Server started at : http://localhost:5010
info: Apr-03-2022 09:08:41: Database Connected and Synced
I found out from the info.log
file that the node server of the app was configured to run on port 5010
in the config/config.json
file of the Node JS project.
All I had to do was to adjust the Docker exposed port from 3000
to 5010
as well as change the port that the Docker container runs from port 3000
to port 5010
. Furthermore, changed the AWS security group of the server where that app is being deployed to allow port 5010
instead of port 3000
and this time everything ran fine.
Upvotes: 1
Reputation: 25792
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 give you the container IP address, not the host IP address.
Upvotes: 3