1

I have an angular app hosted in a docker container. I need to access this UI outside the machine where the container is running. Because of this, I need to call other services on the host machine using the docker host machine's IP address.

For this, I need a way to pass the Docker host's IP to my angular app where the URL to another containerized service is generated.

I tried setting the environment variable and passing it but haven't been successful in finding the right way to go about it.

krr
  • 59
  • 8
  • 1
    It sounds like you need either the `-p` flag ([docs](https://docs.docker.com/config/containers/container-networking/)) or host networking ([docs](https://docs.docker.com/network/host/)). Could you clarify with more information about what you're trying to connect to, and where you're connecting from? – Nick ODell Jul 17 '20 at 02:00
  • My containers are on the same network. They can communicate just fine when they are on the same host. But I access the UI of the application from another machine's browser. So the calls that go out to another service (in another container) from the browser cant use the container name(http::5000) and have to use the docker host's IP. – krr Jul 17 '20 at 03:32
  • Okay, so all you need is the external IP address of the host that the container is running on? – Nick ODell Jul 17 '20 at 04:41
  • yes that's right – krr Jul 17 '20 at 04:42

3 Answers3

0

I tried setting the environment variable and passing it but haven't been successful in finding the right way to go about it.

I think setting an environment variable is the right approach here. There are some ways to get the IP address of the host without cooperation from the host, but they either aren't cross-platform, or they get the address of the bridge device, which I assume isn't what you're looking for.

Here's an example of how to pass the IP from the host to the container using an environment variable. I tested this on Ubuntu 18.04.

docker run -it -e HOST_IP="$(hostname -I)" debian printenv HOST_IP

Explanation: The -e HOST_IP="$(hostname -I)" part gets all of the IP addresses of the host, and puts it in an environment variable named HOST_IP. The debian part is the name of the image I'm using. The printenv HOST_IP is used to print out the value of the HOST_IP environment variable.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Thank you. But is this something I can access from within the Angular app? And can I pass it through the Dockerfile, since I am planning to run the containers using docker-compose – krr Jul 17 '20 at 15:02
  • Sorry, I'm not familiar with Angular. I don't think you can do this through docker-compose, since it doesn't support shell commands: https://github.com/docker/compose/issues/4081 – Nick ODell Jul 17 '20 at 15:53
0

I found the answer to this.

I followed this tutorial along and used webpack bundle to access set environment variables: https://codinglatte.com/posts/angular/using-os-environment-variables-in-angular-with-docker/

Instead of setting the environment variables in Dockerfile, I set it in docker-compose this way:

export somevar = ""
docker-compose $@ build
docker-compose build

docker-compose.yml

ui:

    image: app-ui 

    environment:

    - "host-ip:${somevar}"

halfer
  • 19,824
  • 17
  • 99
  • 186
krr
  • 59
  • 8
-1

Provided you are using Docker for Mac or Docker for Windows, you can use

host.docker.internal

From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac/Windows.

Source: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

Source: https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • Your browser (and especially the browser on a remote machine) won't know this host name; it's not something you can use in an Angular or other browser-based application. – David Maze Jul 17 '20 at 10:34
  • Yes as David said, the problem is that I'll be using a remote machine's browser to access the User Interface, and urls to other services on the docker host are initialized in the user interface. Therefore, I need to access the docker host's IP in my angular app that is containerized. – krr Jul 17 '20 at 15:12