django – Postgres到Ubuntu Docker容器连接不起作用

django – Postgres到Ubuntu Docker容器连接不起作用,第1张

概述[编辑]:我可以在没有任何自己的自定义代码的情况下重现这一点.我刚刚创建了一个运行Ubuntu 14.04的新的Linode映像,并根据 Docker’s website的步骤安装了Docker. 然后我跑了: docker run -d --name db postgres 并且可以看到它在运行: $docker psCONTAINER ID IMAGE [编辑]:我可以在没有任何自己的自定义代码的情况下重现这一点.我刚刚创建了一个运行Ubuntu 14.04的新的linode映像,并根据 Docker’s website的步骤安装了Docker.

然后我跑了:

docker run -d --name db postgres

并且可以看到它在运行:

$docker psCONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               nameS9d335e8fc70b        postgres            "/docker-entrypoint.   7 minutes ago       Up 7 minutes        5432/tcp            db

然后我启动一个交互式Ubuntu容器,链接到db,并尝试通过nc和curl测试链接:

$docker run -it --link db ubuntu /bin/bashroot@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 psCONTAINER ID        IMAGE                           COMMAND                CREATED             STATUS              PORTS                  nameS5838047eb14c        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 migrateTraceback (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",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/postgre@R_419_6983@_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?

我可以直接连接到postgres容器:

$docker exec -it db bashroot@d2d6754430a2:/# cat /etc/hosts172.17.0.42     d2d6754430a2127.0.0.1       localhost::1     localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allroutersroot@d2d6754430a2:/# su postgres$p@R_419_6983@p@R_419_6983@ (9.4.4)Type "help" for help.postgres=#

但不是来自Web容器内部:

# 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.

我缺少什么想法?

解决方法 检查你的postgre@R_419_6983@.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 bashroot@11c767bd3d09:/#  p@R_419_6983@ -h db -U postgresp@R_419_6983@ (9.4.3,server 9.4.4)Type "help" for help.postgres=#

编辑(Docker 1.7.1输出)

$docker rm $(docker ps -a -q)$docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEdjango              latest              29755dd6751b        3 days ago          434.5 MBpostgres            latest              f33438ff9aef        3 days ago          265.5 MB$docker -vDocker version 1.7.1,build 786b29d$docker run -d --name db -d postgres$docker run -it --link db:db django bashroot@11c767bd3d09:/#  p@R_419_6983@ -h db -U postgresp@R_419_6983@ (9.4.3,server 9.4.4)Type "help" for help.postgres=#
总结

以上是内存溢出为你收集整理的django – Postgres到Ubuntu Docker容器连接不起作用全部内容,希望文章能够帮你解决django – Postgres到Ubuntu Docker容器连接不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1195942.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存