1

I have an application running in a Docker container. This application exposes some resources in it's API. These resources must be exposed with their full urls, this includes scheme, host, port and path.

From inside the container, it looks like my IP address (host) is 172.17.0.2. This is probably because the container is part of the internal Docker network.

However, the host has an eth0 device with an IP of 192.168.1.* and this is the one I want to know and include in the url's.

I understand that I can evaluate it when I create the container and pass it as an environment variable, but it would be nice with a solution that does not involve recreating the container if the Docker host IP address changes.

I know that I could set up a service on the host to periodically write the IP address into a volume and then read it from there, however I would like to have as few moving parts as possible outside of the container.

3
  • To make it more clear: do you have to "create" urls inside your container, that look like docker_host_ip/path? (docker_host_ip here means the 192.168.1.* you are mentioning) Commented Jun 21, 2018 at 10:31
  • That is correct. And I know I can get them during HTTP requests in my application, but it would be nice a solution that works in background jobs too. Commented Jun 21, 2018 at 10:54
  • Are you trying to access a service running on the host? If this is the case, your host joins the default docker bridge network (docker0) with the IP 172.17.0.1 which might help you access any service running on the host. Commented Jun 21, 2018 at 10:59

1 Answer 1

2

Launch the docker container with --net=host and all interfaces of your host will be accessible from container, even of course your 192.168.1.X.

Sign up to request clarification or add additional context in comments.

4 Comments

The question says "...this is the one (IP) I want to know and include in the url's". What I understand is that there is a need to include the docker host IP to the urls. How is this solved with just using --net=host?
Haven't tried yet, but I suppose I should be able to iterate over network interfaces in my application and find the correct one.
@tgogos, it works because with --net=host, container maps host's configuration to its. If you do a docker exec -ti <container> bash and execute ifconfig you'll see the same interfaces than outside furthermore docker internal IP.
It should be pointed out though, that with --net=host the network isolation docker can offer is removed and your container shares the same network stack with the host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.