Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tanepiper/6d7b51e803a0e3c88b330a93e9abbf25 to your computer and use it in GitHub Desktop.
Save tanepiper/6d7b51e803a0e3c88b330a93e9abbf25 to your computer and use it in GitHub Desktop.
Running Angular CLI in local dev behind docker nginx reverse proxy

Setup

Angular app talking to multiple microservices using Cookie authentication on ajax requests. Cookies are not passed over cross domain ajax requests, so we need to be able to have the Angular app and microservices running on same-domain in local dev as well as in production.

We have a Docker nginx proxy running on localhost:8000:

  • proxying to multiple microservices on localhost:8000/api/<microservice>
  • proxying other requests to Angular CLI (ng serve) on en0 inet (192.168.1.5 for instance) port 4200
  • using a python script to get the correct en0 IP and write it to the nginx config at proxy startup

Considerations

ng serve needs to be serving on 0.0.0.0 in order to be accessible from the docker container on the en0 inet address. We also need to tell the app where the live reload information is hosted.

Solution

ng serve --host 0.0.0.0 --public-host http://localhost:4200 does this nicely.

If you prefer, you can set these options in the .angular-cli.json:

"defaults": {
  "serve": {
    "host": "0.0.0.0",
    "public-host": "http://localhost:4200"
  }
}

Now you just run ng serve and access your app on localhost:8000 with livereload 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment