[英]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.我有一个简单的 python web 应用程序,如下所示,我试图在 docker 容器中进行容器化并运行。 The endpoints '/' and '/es' works as this is hosted on localhost.
端点 '/' 和 '/es' 工作,因为它托管在本地主机上。
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.我在 IIS 中有一个网站,自定义主机名为www.devtenant1.local:777 ,它映射到我的主机主机文件中的 127.0.0.1。 How can I access this website from docker container which is running on host machine with custom hostname?
如何从使用自定义主机名在主机上运行的 docker 容器访问该网站?
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 -我尝试使用
docker run
或docker build
在容器内添加以下主机条目,但它不起作用 -
Follow steps below.请按照以下步骤操作。
1.Find private Ip using ipconfig command. 1.使用ipconfig命令查找私有IP。 My ip is .
我的ip是。 .
. .
. *
*
2.docker container running on machine 2.docker 容器在机器上运行
3.using docker exec command Go inside container and try ping IP in first step. 3.使用docker exec命令进入容器并在第一步尝试ping IP。
4.So you can see private ip is reachable from inside container. 4.所以你可以看到从容器内部可以访问私有IP。 Because its in same network and docker uses bridge network to create container.
因为它在同一网络中并且 docker 使用桥接网络来创建容器。
5.I can access my iis home page using curl command inside container ip:80 gives 200 response. 5.我可以在容器 ip:80 中使用 curl 命令访问我的 iis 主页给出 200 响应。
6.adding entry in hosts file I am able to ping using FQDN from inside docker. 6.在主机文件中添加条目我可以从docker内部使用FQDN ping。 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.
因为现在私有 ip 被映射到www.devtenant1.local dns.now 我们能够从 docker 容器内部使用 FQDN ping,它指向我在第一步中获得的私有 ip。 Docker run / docker build --add-host=devtenant1.local:10.1.10.160 This will add entry in /etc/hosts file like mentioned below
Docker run / docker build --add-host=devtenant1.local:10.1.10.160 这将在 /etc/hosts 文件中添加条目,如下所述
127.0.0.1 localhost
10.1.10.60 devtenant.local
Just don't include https:// or www part.只是不要包含 https:// 或 www 部分。
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 7.此外,我使用相同的 fqdn www.devtenant1.local (默认端口 80)从我的本地获得响应,在您的情况下是www.devtenant1.local:777
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.