skip to Main Content

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.

enter image description here

When I see the list of files, I am seeing all good.

enter image description here

enter image description here

When I go for http://localhsot/user it returns values

enter image description here

But when I read the value from browser it is showing

enter image description here

enter image description here

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:

enter image description here

2

Answers


  1. 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 Docker localhost 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 using 0.0.0.0:

    app.listen(3000, '0.0.0.0', () => {
      console.log("listening on PORT verify: 3000");
    });
    

    The solution is exemplified in the nodejs guide documentation as well.

    In addition, please, try adjusting the Url variable in your environment files to the address http://localhost, without indicating the port, i.e., using port 80: according to the code you uploaded to your repository in Github your app seems to listen on port 3000, but in your screenshots you are certainly using port 80. The reasoning behind this is that if express is serving both your Angular SPA and your API, and you are able to contact both things from your host, using http://localhost/read and http://localhost/user, respectively, then probably adjusting you Url variable to http://localhost as advised could do the trick.

    Login or Signup to reply.
  2. 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’s localhost 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search