I am trying to setup poc for docker with one simple project. It includes angular, express, nodejs and mongodb atlas.
angular runs on 4200 and nodejs 3000
https://github.com/changan1111/UserManagement
it is working fine in local the same setup..
When I go for docker?
I am seeing that node js is started and running.
When I see the list of files, I am seeing all good.
When I go for http://localhsot/user it returns values
But when I read the value from browser it is showing
i have tried few solutions which is given but all returns same response that failed. what is wrong with this.. can any one look in to this and let me know that what is the problem on the setup.
app.use(cors());
/*
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "req.headers.origin"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "DELETE,PUT,GET,POST");
next();
});*/
/*
app.use(cors({
"origin": ['http://localhost:3000'],
"methods": "GET,PUT,POST",
"preflightContinue": false,
"credentials": true
}));*/
tried with ip address http://192.168.0.103/ as well but no change
Dockerfile:
2
Answers
It is unclear in your question but, according to the images you provided, if seems that your Angular app is unable to reach your Express backend in
127.0.0.1
.You configured
http://localhost:3000
as your API endpoint, but please, be aware that from the point of view of Dockerlocalhost
refers to the container itself, and could be not the same as your actual host.To solve the problem, you could indicate to
Express
that it should listen in every network interface using0.0.0.0
:The solution is exemplified in the nodejs guide documentation as well.
In addition, please, try adjusting the
Url
variable in yourenvironment
files to the addresshttp://localhost
, without indicating the port, i.e., using port80
: according to the code you uploaded to your repository in Github your app seems to listen on port3000
, but in your screenshots you are certainly using port80
. The reasoning behind this is that ifexpress
is serving both your Angular SPA and your API, and you are able to contact both things from your host, usinghttp://localhost/read
andhttp://localhost/user
, respectively, then probably adjusting youUrl
variable tohttp://localhost
as advised could do the trick.Did you try exposing your ports like this when running your container?
docker run -p 4000:4000 container_name
(assuming Linux host here)or just
docker run --network="host" container_name
perhaps?Then access via
localhost:4000
from any service inside or outside Docker.The container’s internal
localhost
will match up with your actual host machine’slocalhost
this way.There are some downsides to this, but I’m assuming you’re not running this in a production setting, so it shouldn’t really matter much.
It isn’t clear whether you’re trying to access your Dockerized app from outside Docker. Are both your backend and frontend sharing the same container? Are they both containerized?
EXPOSE
only allows Docker’s internal apps to find that port IIRC.You need to specify ports to expose to your host.