I've read similar question ng serve not working in Docker container but I don't understand why do I have to specify a host 0.0.0.0 to run ng serve. For comparison, I ran a simple script with express.js in the same container on the same port and in this case the website was served at localhost:8080 without defining 0.0.0.0 host.
As I understand both servers (ng serve and express.js) are working the same way. Both runs inside container but only express can be accessed in browser and I'm confused why ng serve isn't.
Steps to reproduce:
- Run LTS Node.js container with name
angular-testfrom CLI and publish port 8080:4200
docker container run --name angular-test -it -v ${PWD}:/app/ -p 8080:4200 node:lts /bin/bash - connect to container's terminal
docker container attach angular-test - go to angular project and run
ng serve - go to browser
localhost:8080but returns "The connection was reset The connection to the server was reset while the page was loading. ...` - stop
ng serve - run
node test-expressto start express.js server on the same port4200 - go to browser
localhost:8080and website is loaded without any issue, textExpress.js works!is visible
const express = require('express')
const app = express()
const port = 4200
app.get('/', (req, res) => {
res.send('Express.js works!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
It probably doesn't matter but I'm using Docker for Windows with WSL2 (Windows 10 Pro).