1
version: "3.0"
services:
  backend:
    build: ./backend
    ports:
      - "3600:8000"
    depends_on:
      - mysql
  redis:
    image: redis
  mysql:
    image: mysql:5.6
    environment:
      - MYSQL_DATABASE=django-app
      - MYSQL_ROOT_PASSWORD=password

This is my docker-compose.yml

#!/bin/sh

cd src

rm *.sqlite3

python manage.py makemigrations

python manage.py migrate

daphne -b 0.0.0.0 -p 8000 literature.asgi:application

This is my docker entrypoint for the backend. But whenever I do docker-compose up. It fails to migrate the database succesfully and I get the error saying "Can't connect to mysql". But if I restart just the django-app container then it works fine. All migrations are finished and django-admin works.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django-app',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'mysql',
        'PORT': '3306',
    }
}

It works fine if I use SQLite instead.

Deepak Kar
  • 83
  • 2
  • 7
  • https://docs.docker.com/compose/compose-file/#depends_on Try that. Your MySQL probably did not start yet. – Christophe De Troyer Jun 08 '20 at 09:40
  • Have you tried to link the your container exposed port to one of your local machine? -p 8000:8000 – Jorge Mauro Jun 08 '20 at 10:40
  • 2
    What's the actual error? `depends_on:` does not wait for the database to fully start up, and a common problem is the application starting in 5 seconds but the database requiring 30-60. That matches your symptom of it working when you restart the Django app. – David Maze Jun 08 '20 at 10:49
  • @DavidMaze Is there any solution for that? – Deepak Kar Jun 08 '20 at 11:42
  • [Docker Compose wait for container X before starting Y](https://stackoverflow.com/q/31746182/10008173) outlines several paths. – David Maze Jun 08 '20 at 12:20

0 Answers0