简体   繁体   中英

How to connect to a hostname on local IIS from docker container?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM