Skip to content Skip to sidebar Skip to footer

'can\'t Connect To Mysql Server On \'db\' Django-restframework With Mysql In Docker

I have dockerized my existing Django Rest project which uses MySQL database. Dockefile FROM python:3.6 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code COPY . /code/ RUN pip

Solution 1:

This may happen if your application container (web here) starts before your database is fully initialized.

You must wait until db service is started (more precisely, until mysql is accepting connections) before starting your application (python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000).

For that, you can use wait-for-it.sh script (check https://docs.docker.com/compose/startup-order/) before running your python commands. Alternatively, you could use a restart policy on your web service (https://docs.docker.com/compose/compose-file/#restart_policy) to automatically restart your service (until database is up).

Post a Comment for "'can\'t Connect To Mysql Server On \'db\' Django-restframework With Mysql In Docker"