Django random can't connect to mysql

In django I'm getting can't connect to mysql randomly it works fine and just gives it to me and on refresh it works fine, completely random, I can't even reproduce it. any ideas?

compose file:

networks:
  my_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1


services:
  web:
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      my_network:
        ipv4_address: 172.16.238.6
    restart: always
    depends_on:
      - db
    env_file:
      - ./backend/.env
    image: project-api
    build:
      context: .
      dockerfile: backend/Dockerfile.dev
    command: bash -c "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn project.wsgi -b 0.0.0.0:9001"
    volumes:
      - ./backend:/code
      - ./backend/logs:/logs


  db:
    image: mysql:8.0
    command: mysqld --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./backend/data:/var/lib/mysql
      - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      my_network:
        ipv4_address: 172.16.238.9
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: dev_db
      MYSQL_USER: dev_db_user
      MYSQL_PASSWORD: root

Back to Top