我们如何将我们的docker应用程序连接到本地运行的postgres?

我正在寻找一种连接Docker容器应用程序的方法,以便通过https://postgresapp.com/访问Postgres数据库

想知道是否有某些端口可以打开以及yml文件是什么样子,使docker-compose可以在本地运行postgres。

谢谢!

你必须在主机上修改postgresconfiguration文件,特别是

1)pg_hba.conf 2)postgres.conf

你可以在/ var / lib / postgresql / [version] / path中find上面的configuration

首先检查你的docker0网桥接口,它可能是172.17.0.0/16,如果没有相应的更改。

使postgresql.confpath中的更改将与pg_hba.conf相同。

listenaddress到“*”

然后在pg_hba.conf中添加规则为

主机全部为172.17.0.0/16 md5。

然后在docker应用程序中使用主机IP地址连接到在主机上运行的姿态。

从容器内或容器外访问数据库没有区别。

假设Postgres数据库正在localhost上运行。 如果你想从一个小的Python脚本连接到它,你可以这样做,如下所示:

 #!/usr/bin/python import psycopg2 import sys def main(): #Define our connection string conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'" # print the connection string we will use to connect print "Connecting to database\n ->%s" % (conn_string) # get a connection, if a connect cannot be made an exception will be raised here conn = psycopg2.connect(conn_string) # conn.cursor will return a cursor object, you can use this cursor to perform queries cursor = conn.cursor() print "Connected!\n" if __name__ == "__main__": main() 

为了运行这个脚本,你需要在你的本地机器上安装Python和psycopg2,可能是在虚拟环境中。 或者,你可以把它放在一个容器中。 而不是手动安装所有东西,你可以用你的安装说明来定义一个Dockerfile。 它可能看起来像这样:

 FROM python:latest ADD python-script.py /opt/www/python-script.py RUN pip install psycopg2 CMD ["python", "/opt/www/python-script.py"] 

如果我们build立和运行…

 dave-mbp:Desktop dave$ ls -la -rw-r--r-- 1 dave staff 135 Dec 8 19:05 Dockerfile -rw-r--r-- 1 dave staff 22 Dec 8 19:04 python-script.py dave-mbp:Desktop dave$ docker build -t python-script . Sending build context to Docker daemon 17.92kB Step 1/4 : FROM python:latest latest: Pulling from library/python 85b1f47fba49: Already exists ba6bd283713a: Pull complete 817c8cd48a09: Pull complete 47cc0ed96dc3: Pull complete 4a36819a59dc: Pull complete db9a0221399f: Pull complete 7a511a7689b6: Pull complete 1223757f6914: Pull complete Digest: sha256:db9d8546f3ff74e96702abe0a78a0e0454df6ea898de8f124feba81deea416d7 Status: Downloaded newer image for python:latest ---> 79e1dc9af1c1 Step 2/4 : ADD python-script.py /opt/www/python-script.py ---> 21f31c8803f7 Step 3/4 : RUN pip install psycopg2 ---> Running in f280c82d74e7 Collecting psycopg2 Downloading psycopg2-2.7.3.2-cp36-cp36m-manylinux1_x86_64.whl (2.7MB) Installing collected packages: psycopg2 Successfully installed psycopg2-2.7.3.2 ---> bd38f911bb6a Removing intermediate container f280c82d74e7 Step 4/4 : CMD python /opt/www/python-script.py ---> Running in 159b70861893 ---> 4aa783be5c90 Removing intermediate container 159b70861893 Successfully built 4aa783be5c90 Successfully tagged python-script:latest dave-mbp:Desktop dave$ docker run python-script >> Connected! 

容器主要是一个包装解决scheme。 我们将能够连接到外部数据库或API端点,就像我们正在运行一个容器外的所有东西一样。

有一件事要记住,虽然… localhost可能会解决的虚拟机,如果你使用docker工具箱/ virtualbox。 在这种情况下,除非在虚拟机中运行桥接networking连接,否则不会连接到localhost 。 否则,只需指定主机IP而不是localhost

Postgres的默认端口是5432

Docker提供了不同的联网模式 。 我可以告诉你如何使用网桥模式(默认使用)实现你的目标。

总是有一个桥接networking,允许你访问主机。 它是默认创build的,名为docker0

在terminal ip addr show docker0运行以下命令,查看您的主机ip是否可用于您运行的所有容器。

在我的机器上输出:

 developer@dlbra:~$ ip addr show docker0 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:b4:95:60:89 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever 

因此,在docker-compose.yml中不需要任何额外的configuration您只需将您的db主机configuration为您所看到的IP地址即可。 在我的情况下,它将是172.17.0.1

如果您的本地安装的Postgres侦听不同的端口,那么在您的应用程序configuration中也指定该端口。