简体   繁体   中英

Why am I unable to access my node.js app within a docker container?

I am running two containers on an ubuntu linux system in the microsoft azure cloud (a node.js app called eva and a postgres-db used by the app):

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
339b252e9895        eva                 "node app.js"            17 hours ago        Up 17 hours         0.0.0.0:8086->8086/tcp   eva
91a39d88c1eb        postgres            "/usr/lib/postgresql/"   19 hours ago        Up 19 hours         5432/tcp                 postgres

I exposed the node.js app on port 8086 and used the following command to start the container from the image:

sudo docker run --name eva -p 8086:8086 eva

I have opened the port for this linux server on the azure console and tried to access it but chrome shows me a connection refused error. I also tried to access it locally via curl:

sudo curl --trace curl.out localhost:8086

This is curl.out:

== Info: Rebuilt URL to: localhost:8086/
== Info:   Trying 127.0.0.1...
== Info: Connected to localhost (127.0.0.1) port 8086 (#0)
=> Send header, 78 bytes (0x4e) 0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1.. 0010: 48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 3a Host: localhost: 0020: 38 30 38 36 0d 0a 55 73 65 72 2d 41 67 65 6e 74 8086..User-Agent 0030: 3a 20 63 75 72 6c 2f 37 2e 34 37 2e 30 0d 0a 41 : curl/7.47.0..A 0040: 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a       ccept: */*....
== Info: Recv failure: Connection reset by peer
== Info: Closing connection 0

I have already done the same locally on my MacOS and it was working. Additionally I deployed the app myself by moving the source files to the linux system via scp and ran the app myself on port 8085 by only using the node command; it was working, too.

Why am I unable to access my node.js app within a docker container on ubuntu?

Alright, tanks a lot to fernandezcuesta!

I was able to find the answer myself by using your suggested command. When running docker exec -it eva ss -tnlp I realized that eva was still listening on port 8085 (which this instance actually could not use since another instance of the app was using this port already).

After taking a look into the .env-file of my source code I realized that the port parameter was still set to 8085. That caused the http-server that is set up by the node.js app to listen to 8085.

After changing the parameter and deploying the app again I've been able to see the app in chrome on port 8086.

I do not know why it worked on MacOS in this way, though... But it works on my remote machine now so I am happy :)

Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM