docker-compose django-postgres error



我完全按照本教程(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实例时,它工作得很好。

指针吗?

我遇到了同样的问题,如果你再次运行相同的命令,那么它就可以工作了。这几乎就像"docker-compose up"在同一时间带来两个容器和django应用程序容器试图运行,而postgres容器仍在设置?这太令人困惑了。: * (

更新:

看来我的怀疑是对的,去https://github.com/docker/compose/issues/374看看吧。

有点粗糙,但是有一个简单的方法可以解决这个竞争条件,那就是让django app容器在运行命令之前休眠几秒钟,这样这些容器所依赖的服务,比如PostgreSQL,就可以准备好接受连接了。例如:command: bash -c "sleep 3 && python manage.py runserver 0.0.0.0:8000"在django服务下的yml文件中,为docker-compose.

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

相关内容

  • 没有找到相关文章

最新更新