Home > Net >  reachability problem with docker node container and angular/nodejs
reachability problem with docker node container and angular/nodejs

Time:01-19

To follow the angular tutorial available on the angular website, I decided to download the "node" image from dockerhub. I mapped the container ports 4200 and 8080 to the physical windows machine ports 4200 and 8888 respectively, with the following command:

docker container run -it -p 8888:8080 -p 4200:4200 -d --name nodejsdocker -v C:\root\docker\nodejs:/home/node node  bash

inside the container I ran the following commands:

npm install -g @angular/cli
ng new my-app
ng serves --open

and I see the application starting on port 4200:

root@99a0f1e2bc80:/# netstat -anp |grep 4200
tcp        0      0 127.0.0.1:4200          0.0.0.0:*               LISTEN      124/ng serve --open

at this point, on the physical windows machine, I expect to be able to make the request via browser at the following url http://localhost:4200/ but the request returns "time out"

wget also works inside the container:

root@99a0f1e2bc80:/# wget http://localhost:4200/
--2023-01-18 16:34:01-- http://localhost:4200/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:4200... connected.
HTTP request sent, awaiting response... 200 OK
Length: 561 [text/html]
Saving to: 'index.html.2'

index.html.2 100%[=========================================== =================================================== =======================>] 561 --.-KB/s to 0s

2023-01-18 16:34:01 (49.5 MB/s) - 'index.html.2' saved [561/561]

root@99a0f1e2bc80:/#

Why am I not able to send the HTTP request http://localhost:4200/ on the physical machine? I'm expecting to receive an html index page from the container angular app.

CodePudding user response:

ng serve starts a dev server and allows only connections from localhost. But Docker adds a proxy between container and your host system and routes the connection.

You can set --host 0.0.0.0 to allow all hosts.

More options: https://angular.io/cli/serve

CodePudding user response:

I didn't want to mess up my machine, that's why I thought of docker. I did the same thing with mysql and it worked. I've only used docker run without docker file

  • Related