Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Postgres到Ubuntu Docker容器链接不工作_Django_Postgresql_Docker - Fatal编程技术网

Django Postgres到Ubuntu Docker容器链接不工作

Django Postgres到Ubuntu Docker容器链接不工作,django,postgresql,docker,Django,Postgresql,Docker,[编辑]:我可以在不使用任何自定义代码的情况下复制此文件。我刚刚创建了一个运行Ubuntu14.04的全新Linode映像,并按照上的步骤安装了Docker 然后我跑: docker run -d --name db postgres 可以看到它正在运行: $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS

[编辑]:我可以在不使用任何自定义代码的情况下复制此文件。我刚刚创建了一个运行Ubuntu14.04的全新Linode映像,并按照上的步骤安装了Docker

然后我跑:

docker run -d --name db postgres
可以看到它正在运行:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
9d335e8fc70b        postgres            "/docker-entrypoint.   7 minutes ago       Up 7 minutes        5432/tcp            db
然后我启动一个交互式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) Failed to connect to 172.17.0.2 port 5432: Connection timed out
我错过了什么


我正在尝试将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 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      
并且似乎正确地联系在一起:

$ docker inspect -f "{{ .HostConfig.Links }}" web
[/db:/web/db]
但是,当我尝试在web容器中运行“python manage.py migrate”时,它似乎无法连接到postgres容器:

# 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?
# 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
但不是从web容器内部:

# 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?
# 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
我的主机运行的是Ubuntu 14.04


你知道我遗漏了什么吗?

检查你的
postgresql.conf
是否有
listen\u地址='*'

默认值为
listen\u 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, server 9.4.4)
Type "help" for help.

postgres=#
编辑(Docker 1.7.1输出)


看起来不错:root@bc772503cb1e:/#cat/var/lib/postgresql/data/postgresql.conf | grep-listen_-addresses-listen_-addresses='*'#要侦听的IP地址;看起来您使用的是自定义Postgres容器,而不是Docker Hub中的容器,对吗?你能发布你的pg_hba.conf吗?不,我使用的是DockerHub中的一个,没有做任何更改,它是在我执行run命令时下载的。pg_hba.conf中唯一未注释的行是:
host all 0.0.0.0/0 trust
Django容器中的最后一个命令postgres超时之前,所有操作都有效:root@e03db6d124ee:/#psql-h db-U postgres psql:无法连接到服务器:连接超时服务器是否在主机“db”上运行(172.17.0.26)并在端口5432上接受TCP/IP连接?奇怪。也许1.7.1在Ubuntu 14.04上运行不好?现在我降级了,这对我很有效,所以我要继续前进。