2

I have a simple python web application like below which I am trying to containerize in a docker container and run. The endpoints '/' and '/es' works as this is hosted on localhost.

I have a website in IIS with custom hostname as www.devtenant1.local:777 which maps to 127.0.0.1 in my host machine hosts file. How can I access this website from docker container which is running on host machine with custom hostname?

Code below

import json
from flask import Flask
import requests

app = Flask(__name__)


@app.route("/") *#this works*
def main():
    return "Welcome!"


@app.route("/es") *#this works*
def connect_to_es():
    result = requests.get("http://host.docker.internal:9200/")
    return result.json()


@app.route("/values") *#this doesn't work. What should be my URL in get() below*
def get_values():
    headers = {'content-type': 'application/json'}
    result = requests.get("https://www.devtenant1.local:777/api/values", headers=headers, verify=False)
    return result.json()

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')

I tried adding below host entries inside the container using docker run or docker build but it doesn't work -

  • --add-host=www.devtenant1.local:172.17.0.1 (Gateway)
  • --add-host=www.devtenant1.local:172.17.0.2 (IPAddress)
  • --add-host=www.devtenant1.local:0.0.0.0 (from one of the suggestions)
  • --add-host=www.devtenant1.local:10.1.10.160 (from one of the suggestions)
Deepak Agarwal
  • 458
  • 1
  • 4
  • 18
  • You probably have to add an entry to the hosts file within the container that points to your host machine. I assume you don't have a central DNS that is being used on your host's network. – Antebios Oct 16 '20 at 20:49
  • Thanks @Antebios. Yes, I need to add entry in the host file within the container, however, I'm trying to figure out what it would be. I tried below but doesn't work (either from docker build or docker run commands). --add-host=www.devtenant1.local:172.17.0.1 --add-host=www.devtenant1.local:172.17.0.2 --add-host=www.devtenant1.local:0.0.0.0 --add-host=www.devtenant1.local:10.1.10.160 Updated original post with this – Deepak Agarwal Oct 17 '20 at 06:48
  • I think it is possible that docker has problems connecting to the local host network. You can try to switch the bridge mode and then access the local IIS. The complete process can refer to this answer.https://stackoverflow.com/a/24326540/14162739 – Bruce Zhang Oct 22 '20 at 06:19

2 Answers2

0

Follow steps below.

1.Find private Ip using ipconfig command. My ip is ...*

2.docker container running on machine

3.using docker exec command Go inside container and try ping IP in first step.

4.So you can see private ip is reachable from inside container. Because its in same network and docker uses bridge network to create container.

5.I can access my iis home page using curl command inside container ip:80 gives 200 response.

6.adding entry in hosts file I am able to ping using FQDN from inside docker. Because now private ip is mapped to www.devtenant1.local dns.now we able to ping using FQDN from inside docker container, which is pointing to my private ip got in first step. Docker run / docker build --add-host=devtenant1.local:10.1.10.160 This will add entry in /etc/hosts file like mentioned below

127.0.0.1 localhost
10.1.10.60 devtenant.local

Just don’t include https:// or www part.

7.Also I am getting response from my local using same fqdn www.devtenant1.local (port 80 default) which in your case is www.devtenant1.local:777

Bhushan Gholave
  • 157
  • 2
  • 9
  • Thanks @Bhushan. This works for port 80 but I didn't find it to be working for other ports mentioned in my code. Suggest if you can set this up as test websites and container on your end and let me know if the container works the way it intends to from the browser or outside the container – Deepak Agarwal Oct 17 '20 at 14:50
  • I am sure you can reach any port on your local if it is serving. – Bhushan Gholave Oct 17 '20 at 15:35
0
docker run <image> --add-host "www.devtenant1.local:host-gateway"

Or in case of docker-compose:

extra_hosts:
  - "www.devtenant1.local:host-gateway"
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 19 '23 at 21:58