连接 Airflow Docker 到 PostgreSql Docker
Connection Airflow Docker to PostgreSql Docker
我在 Docker 中使用 Airflow 和 PostgreSQL。
所以我在端口 5433.Container (384eaa7b6efb) 上设置了一个 PostgreSQL 数据库。这是我想要使用 Airflow 中的 dag 获取数据的地方。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
384eaa7b6efb postgres "docker-entrypoint.s…" 4 hours ago Up 4 hours
0.0.0.0:5433->5432/tcp test-instance
421d443540fb apache/airflow:2.0.1 "/usr/bin/dumb-init …" 18 hours ago Up 4 hours (healthy)
0.0.0.0:8080->8080/tcp airflow_docker-airflow-webserver-1
ff4bea4f16dd apache/airflow:2.0.1 "/usr/bin/dumb-init …" 18 hours ago Up 4 hours
8080/tcp airflow_docker-airflow-scheduler-1
4cead3ee6667 postgres:13 "docker-entrypoint.s…" 18 hours ago Up 4 hours (healthy)
5432/tcp airflow_docker-postgres-1
8bb2cefd456e apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Up 5 hours (healthy)
0.0.0.0:5555->5555/tcp, 8080/tcp airflow_docker-flower-1
5c4b96d9c5a0 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Up 5 hours (healthy)
8080/tcp airflow_docker-airflow-worker-1
3442eae78844 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Restarting (1) 11 seconds ago airflow_docker-airflow-triggerer-1
7e945051435f redis:latest "docker-entrypoint.s…" 4 days ago Up 5 hours (healthy)
6379/tcp airflow_docker-redis-1
我在 PgAdmin 上创建了一个新的服务器连接来访问我的数据库:
我可以看到我的数据,一切正常。
所以现在我创建了这个 dag 来查询该数据库中的数据:
def queryPostgresql():
conn_string="dbname='postgres' host='localhost' port='5433' user='postgres' password='admin'"
conn=db.connect(conn_string)
df=pd.read_sql("select name,city from public.users",conn)
df.to_csv('postgresqldata.csv')
print("-------Data Saved------")
当我运行气流中的阻力时,
[2021-10-17 14:37:16,485] {taskinstance.py:1455} ERROR - could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5433?
我在这里做错了什么?这是我应该添加连接的方式吗?
简答
将主机更改为; host.docker.internal
.
长答案
这取决于您使用的Os。为了从容器内访问主机的网络,您需要在 docker 中使用主机的 IP 地址。方便的是,在 Windows 和 Max 上,这是使用容器内的域 host.docker.internal
解决的。如 docker's documentation 中指定:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network access). We recommend that you 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.
linux 中也有解决此问题的方法,已在
What is linux equivalent of "host.docker.internal"
我的配置几乎一样。如果您的所有容器都在同一个网络中,您可以通过 <postgres-container-name>:<port>
(在您的情况下 test-instance:5433
)
访问您的数据库
我在 Docker 中使用 Airflow 和 PostgreSQL。
所以我在端口 5433.Container (384eaa7b6efb) 上设置了一个 PostgreSQL 数据库。这是我想要使用 Airflow 中的 dag 获取数据的地方。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
384eaa7b6efb postgres "docker-entrypoint.s…" 4 hours ago Up 4 hours
0.0.0.0:5433->5432/tcp test-instance
421d443540fb apache/airflow:2.0.1 "/usr/bin/dumb-init …" 18 hours ago Up 4 hours (healthy)
0.0.0.0:8080->8080/tcp airflow_docker-airflow-webserver-1
ff4bea4f16dd apache/airflow:2.0.1 "/usr/bin/dumb-init …" 18 hours ago Up 4 hours
8080/tcp airflow_docker-airflow-scheduler-1
4cead3ee6667 postgres:13 "docker-entrypoint.s…" 18 hours ago Up 4 hours (healthy)
5432/tcp airflow_docker-postgres-1
8bb2cefd456e apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Up 5 hours (healthy)
0.0.0.0:5555->5555/tcp, 8080/tcp airflow_docker-flower-1
5c4b96d9c5a0 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Up 5 hours (healthy)
8080/tcp airflow_docker-airflow-worker-1
3442eae78844 apache/airflow:2.2.0 "/usr/bin/dumb-init …" 4 days ago Restarting (1) 11 seconds ago airflow_docker-airflow-triggerer-1
7e945051435f redis:latest "docker-entrypoint.s…" 4 days ago Up 5 hours (healthy)
6379/tcp airflow_docker-redis-1
我在 PgAdmin 上创建了一个新的服务器连接来访问我的数据库:
我可以看到我的数据,一切正常。
所以现在我创建了这个 dag 来查询该数据库中的数据:
def queryPostgresql():
conn_string="dbname='postgres' host='localhost' port='5433' user='postgres' password='admin'"
conn=db.connect(conn_string)
df=pd.read_sql("select name,city from public.users",conn)
df.to_csv('postgresqldata.csv')
print("-------Data Saved------")
当我运行气流中的阻力时,
[2021-10-17 14:37:16,485] {taskinstance.py:1455} ERROR - could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5433?
我在这里做错了什么?这是我应该添加连接的方式吗?
简答
将主机更改为; host.docker.internal
.
长答案
这取决于您使用的Os。为了从容器内访问主机的网络,您需要在 docker 中使用主机的 IP 地址。方便的是,在 Windows 和 Max 上,这是使用容器内的域 host.docker.internal
解决的。如 docker's documentation 中指定:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network access). We recommend that you 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.
linux 中也有解决此问题的方法,已在 What is linux equivalent of "host.docker.internal"
我的配置几乎一样。如果您的所有容器都在同一个网络中,您可以通过 <postgres-container-name>:<port>
(在您的情况下 test-instance:5433
)