gpt4 book ai didi

django - docker-compose django-postgres错误

转载 作者:行者123 更新时间:2023-12-02 19:12:45 25 4
gpt4 key购买 nike

我完全按照本教程(http://docs.docker.com/compose/django/)进行操作,但是在执行docker-compose up时出现此错误:

$ docker-compose up
Recreating composeexample_db_1...
Recreating composeexample_web_1...
Attaching to composeexample_db_1, composeexample_web_1
db_1 | LOG: database system was shut down at 2015-07-27 16:17:21 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | Unhandled exception in thread started by <function wrapper at 0x7f43f0126ed8>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
web_1 | self.check_migrations()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 164, in check_migrations
web_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
web_1 | self.loader = MigrationLoader(self.connection)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
web_1 | self.build_graph()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 182, in build_graph
web_1 | self.applied_migrations = recorder.applied_migrations()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
web_1 | self.ensure_schema()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
web_1 | if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
web_1 | cursor = self.make_debug_cursor(self._cursor())
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
web_1 | six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, async=async)
web_1 | django.db.utils.OperationalError: could not connect to server: No route to host
web_1 | Is the server running on host "db" (172.17.0.19) and accepting
web_1 | TCP/IP connections on port 5432?

现在,这仅在我的Fedora 21系统上发生(内核= 4.0.4-202.fc21.x86_64),但是当我在Virtualbox中启动Ubuntu实例时,它就可以正常工作。

有指针吗?

最佳答案

如果您再次运行相同的命令,那么我也遇到了相同的问题和FWIW,它可以正常工作。好像“docker-compose up”同时启动两个容器,而django app容器尝试在仍设置postgres容器的情况下运行吗?真是令人困惑。 :*(

更新:

我的怀疑似乎是对的,请阅读https://github.com/docker/compose/issues/374

对于这种竞争情况,有些粗略但简单的解决方法是让django应用程序容器在运行命令之前休眠几秒钟,以便这些容器所依赖的服务(例如) PostgreSQL,准备接受连接。例如:
yml文件中django服务下的command: bash -c "sleep 3 && python manage.py runserver 0.0.0.0:8000",用于docker-compose。

另一种选择是用gunicorn或uwsgi运行django服务,直到收到请求,该服务才实例化django应用程序。

希望这可以帮助当前与docker作战的其他人。

关于django - docker-compose django-postgres错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com