Postgres到Ubuntu Docker容器连接不起作用 [英] Postgres to Ubuntu Docker container linking not working
问题描述
:没有任何我自己的自定义代码,我可以重现这个。我刚刚创建了运行Ubuntu 14.04的新的Linode映像,并根据 Docker的网站上的步骤安装了Docker
然后我运行:
docker run -d - name db postgres
可以看到它正在运行:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。7分钟前b $ b
然后我启动一个交互式Ubuntu容器,链接到db,并尝试通过nc和curl测试链接:
$ docker run -it --link db ubuntu / bin / bash
root @ eb02f4e7b89e:/#apt -get install curl
...
root @ eb02f4e7b89e:/#curl http:// $ DB_PORT_5432_TCP_ADDR:$ DB_PORT_5432_TCP_PORT /
curl:(7)无法连接到172.17.0.2端口5432:连接超时
我缺少什么?
我试图将postgres容器与运行Django的应用程序容器相链接似乎没有正确链接。
启动容器的命令是:
$ docker run -d --name db postgres
$ docker run -d --name web --link db -p 8000:80 test_image
两个容器似乎运行正常:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5838047eb14c test_image/test/.docker/st 40分钟前Up 40 minutes 0.0.0.0:8000->80/tcp web
d2d6754430a2 postgres/ docker-entrypoint。 44分钟前Up 44 minutes 5432 / tcp db
似乎已正确链接:
$ docker inspect -f{{.HostConfig.Links}}web
[/ db:/ web / db]
但是,当我尝试在Web容器中运行python manage.py migrate时,它不会似乎能够连接到postgres容器:
#python manage.py migrate
追溯(最近的呼叫最后):
文件manage.py,第10行在< module>
execute_from_command_line(sys.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第338行,execute_from_command_line
utility.execute()
文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第330行,执行
self.fetch_command (子命令).run_from_argv(self.argv)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第390行,在run_from_argv
self.execute(* args,** cmd_options)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py,第441行,执行
output = self.handle(* args,** options)
文件/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py,第93行,在句柄
executor = MigrationExecutor(connection,self.migration_progress_callback)
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py ,第19行,__init__
self.loader = MigrationLoader(self.connection)
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py,第47行,__init__
self.build_graph()
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py,第180行,build_graph
self.applied_migrations = recorder.applied_migrations()
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py,第59行,apply_migrations
self.ensure_schema ()
文件/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py,第49行,在ensure_schema
如果self.Migration._meta。 self.connection.introspection.table_names(self.connection.cursor())中的db_table:
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base。 py,第164行,光标
cursor = self.make_cursor(self._cursor())
文件/usr/local/lib/python2.7/dist-packages/django/db/backends /base/base.py,第135行,在_cursor $ b $中b self.ensure_connection()
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py,第130行,ensure_connection
自我.connect()
文件/usr/local/lib/python2.7/dist-packages/django/db/utils.py,第97行,__exit__
six.reraise(dj_exc_type,dj_exc_value ,traceback)
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py,第130行,在ensure_connection
self.connect( )
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py,第119行,连接
self.connection = self。 get_new_connection(conn_params)
文件/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py,第172行,get_new_connection
connection =数据库.connect(** conn_params)
文件/usr/lib/python2.7/dist-packages/psycopg2/__init__.py,第179行,连接
connection_factory = connection_factory,async = async)
django.db.utils.Oper ationalError:无法连接到服务器:连接超时
服务器是否在主机db(172.17.0.42)上运行,并接受
端口5432上的TCP / IP连接?
我可以直接连接到postgres容器:
$ docker exec -it db bash
root @ d2d6754430a2:/#cat / etc / hosts
172.17.0.42 d2d6754430a2
127.0 .0.1 localhost
:: 1 localhost ip6-localhost ip6-loopback
fe00 :: 0 ip6-localnet
ff00 :: 0 ip6-mcastprefix
ff02 :: 1 ip6-allnodes
ff02 :: 2 ip6-allrouters
root @ d2d6754430a2:/#su postgres
$ psql
psql(9.4.4)
输入help作为帮助。
postgres =#
但不能从Web容器内部: p>
#curl http:// $ DB_PORT_5432_TCP_ADDR:$ DB_PORT_5432_TCP_PORT /
curl:(7)无法连接到172.17.0.42端口5432:连接超时
我的主机正在运行Ubuntu 14.04。
任何想法我都缺少什么?
检查你的 postgresql.conf
有 listen_addresses ='*'
默认值为 listen_addresses ='localhost'
,将以您所描述的方式表现。
编辑:
这适用于我,它适用于你吗?
$ docker pull postgres
$ docker pull django
$ docker run -d --name db -d postgres
$ docker run -it --link db:db django bash
root @ 11c767bd3d09:/#psql -h db -U postgres
psql(9.4.3,服务器9.4.4)
键入h elp的帮助。
postgres =#
编辑(Docker 1.7.1输出) p>
$ docker rm $(docker ps -a -q)
$ docker图像
创建标签图像ID VIRTUAL SIZE
django最新29755dd6751b 3天前434.5 MB
postgres最新的f33438ff9aef 3天前265.5 MB
$ docker -v
Docker版本1.7.1,build 786b29d
$ docker run -d --name db -d postgres
$ docker run -it --link db:db django bash
root @ 11c767bd3d09:/#psql -h db -U postgres
psql(9.4.3,服务器9.4.4)
输入help作为帮助。
postgres =#
[EDIT]: I'm able to reproduce this without any of my own custom code. I just created a fresh Linode image running Ubuntu 14.04 and installed Docker according to the steps on Docker's website.
I then ran:
docker run -d --name db postgres
and can see it running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d335e8fc70b postgres "/docker-entrypoint. 7 minutes ago Up 7 minutes 5432/tcp db
Then I start an interactive Ubuntu container, linked to db, and try to test the link via nc and curl:
$docker run -it --link db ubuntu /bin/bash
root@eb02f4e7b89e:/# apt-get install curl
...
root@eb02f4e7b89e:/# curl http://$DB_PORT_5432_TCP_ADDR:$DB_PORT_5432_TCP_PORT/
curl: (7) Failed to connect to 172.17.0.2 port 5432: Connection timed out
What am I missing?
I'm trying to link a postgres container with an app container running Django but it doesn't seem to be linking properly.
The commands to start the containers were:
$ docker run -d --name db postgres
$ docker run -d --name web --link db -p 8000:80 test_image
Both containers appear to be running fine:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5838047eb14c test_image "/test/.docker/st 40 minutes ago Up 40 minutes 0.0.0.0:8000->80/tcp web
d2d6754430a2 postgres "/docker-entrypoint. 44 minutes ago Up 44 minutes 5432/tcp db
And seem to be correctly linked:
$ docker inspect -f "{{ .HostConfig.Links }}" web
[/db:/web/db]
However, when I try to run "python manage.py migrate" in the web container it doesn't seem to be able to connect to the postgres container:
# python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 93, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 19, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 180, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 164, in cursor
cursor = self.make_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 172, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection timed out
Is the server running on host "db" (172.17.0.42) and accepting
TCP/IP connections on port 5432?
I'm able to connect to the postgres container directly:
$ docker exec -it db bash
root@d2d6754430a2:/# cat /etc/hosts
172.17.0.42 d2d6754430a2
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@d2d6754430a2:/# su postgres
$ psql
psql (9.4.4)
Type "help" for help.
postgres=#
But not from inside the web container:
# curl http://$DB_PORT_5432_TCP_ADDR:$DB_PORT_5432_TCP_PORT/
curl: (7) Failed to connect to 172.17.0.42 port 5432: Connection timed out
My host machine is running Ubuntu 14.04.
Any ideas what I'm missing?
Check that your postgresql.conf
has listen_addresses = '*'
The default is listen_addresses = 'localhost'
, which would behave in the manner you describe.
Edit:
This works for me, does it work for you?
$ docker pull postgres
$ docker pull django
$ docker run -d --name db -d postgres
$ docker run -it --link db:db django bash
root@11c767bd3d09:/# psql -h db -U postgres
psql (9.4.3, server 9.4.4)
Type "help" for help.
postgres=#
Edit (Docker 1.7.1 output)
$ docker rm $(docker ps -a -q)
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
django latest 29755dd6751b 3 days ago 434.5 MB
postgres latest f33438ff9aef 3 days ago 265.5 MB
$ docker -v
Docker version 1.7.1, build 786b29d
$ docker run -d --name db -d postgres
$ docker run -it --link db:db django bash
root@11c767bd3d09:/# psql -h db -U postgres
psql (9.4.3, server 9.4.4)
Type "help" for help.
postgres=#
这篇关于Postgres到Ubuntu Docker容器连接不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!