New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose getting started race condition #416

Closed
jeffre opened this Issue Feb 7, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@jeffre

jeffre commented Feb 7, 2017

Following the Get Started guide, the bash bin/build-app step consistently executes before the postgres container has completed its own startup. The result is an incomplete execution of bin/build-app.

Running bin/build-app a second time after postgres has had a chance to come online, completes successfully.

cabot $ docker-compose run --rm web bash bin/build-app
Creating cabot_db_1
Creating cabot_redis_1
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 93, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 191, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python2.7/site-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/site-packages/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/usr/local/lib/python2.7/site-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/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
	Is the server running on host "db" (172.20.0.2) and accepting
	TCP/IP connections on port 5432?
@dbuxton

This comment has been minimized.

Show comment
Hide comment
@dbuxton

dbuxton Feb 7, 2017

Contributor

Thanks for this. I don't know if there's a better way than the solutions suggested in these two posts:

Any opinions?

Contributor

dbuxton commented Feb 7, 2017

Thanks for this. I don't know if there's a better way than the solutions suggested in these two posts:

Any opinions?

@jeffre

This comment has been minimized.

Show comment
Hide comment
@jeffre

jeffre Feb 7, 2017

@dbuxton I use flask, so my advice may not translate 1:1 with django. But if you import OperationalError from sqlalchemy.exc you can catch that the db isn't online and then do a retry loop.

Along these same lines, you can also import ProgrammingError from sqlalchemy.exc to catch if the db is online but the tables are not yet created.

The side bonus here would be that you could then eleminate the need for these two steps altogher in the "Get Started':
docker-compose run --rm web bash bin/build-app
docker-compose run --rm web python manage.py createsuperuser

jeffre commented Feb 7, 2017

@dbuxton I use flask, so my advice may not translate 1:1 with django. But if you import OperationalError from sqlalchemy.exc you can catch that the db isn't online and then do a retry loop.

Along these same lines, you can also import ProgrammingError from sqlalchemy.exc to catch if the db is online but the tables are not yet created.

The side bonus here would be that you could then eleminate the need for these two steps altogher in the "Get Started':
docker-compose run --rm web bash bin/build-app
docker-compose run --rm web python manage.py createsuperuser

@frankh frankh closed this Feb 9, 2017

@frankh

This comment has been minimized.

Show comment
Hide comment
@frankh

frankh Feb 9, 2017

Contributor

This should be fixed. Merged in a PR to wait for database and broker containers to be running in docker-entrypoint.sh

Contributor

frankh commented Feb 9, 2017

This should be fixed. Merged in a PR to wait for database and broker containers to be running in docker-entrypoint.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment