1

I have exactly followed this tutorial (http://docs.docker.com/compose/django/) but when doing docker-compose up I get this error:

$ 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?

Now, this only happens on my Fedora 21 system(kernel=4.0.4-202.fc21.x86_64) but when I spin up an Ubuntu instance in Virtualbox, it works just fine.

Any pointers?

1 Answer 1

2

I've run into the same issue and FWIW if you run the same command again then it works. It's almost as if "docker-compose up" brings up two containers at the same time and the django app container tries to run while postgres container is still being set up? It's mad confusing. :*(

UPDATE:

It seems that my suspicion was right, have a read at https://github.com/docker/compose/issues/374 .

Somewhat crude but a simple workaround to this race condition is to let django app container to sleep for a few seconds before running the command, so that the services that these containers depend on, e.g. PostgreSQL, are ready to accept connections. For example: command: bash -c "sleep 3 && python manage.py runserver 0.0.0.0:8000" under django service in your yml file for docker-compose.

Another alternative is to run the django service with gunicorn or uwsgi which doesn't seem to instantiate the django application until request is received.

Hope this helps others currently fighting with docker.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.