django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
Asked Answered
A

4

16

This has already been asked, but none of the answers have helped me. This is my configuration. Im running docker-compose with two services, a web app in django and the database in mariadb. I can connect normally to my local db with this exact configuration, only changing the host in settings.py to localhost. When i run docker-compose up, the web service stops immediately after trying to connect to the database, and return this error.

django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")

Dockerfile

FROM python:3.7

RUN mkdir /app

COPY requierments.txt /app/

WORKDIR /app

RUN pip install -r requierments.txt

COPY . /app/

docker-compose.yml

version: '3'

services:
  db:
    image: mariadb:10.2
    expose:
      - 3306
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: 'django_backend'
      MYSQL_USER: 'django'
      MYSQL_PORT: '3306'
      MYSQL_PASSWORD: 'mysql1234pass'
      MYSQL_ROOT_PASSWORD: 'password'

  web:
    build: .
    image: backendblockchain_web
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db
    command:
      - /bin/bash
      - -c
      - |
        sleep 10
        python3 manage.py migrate
        python3 manage.py runserver 0.0.0.0:8000

setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_backend',
        'USER': 'django',
        'PASSWORD': 'mysql1234pass',
        'HOST': 'db',
        'PORT': '3306'
    }
}
Audiophile answered 24/10, 2019 at 18:21 Comment(3)
Run docker ps to see the container name of the DB and use it as the database host for Django.Headstrong
With the name of the service is not enough? The name of the container changes everytime i do docker-compuse up, unless I specify one.Protium
It should stay the same, something like folder_db_1 but you can also set a static host name in the YAML. The service name is not part of the DNS resolution.Headstrong
R
13

your WEB containers starts before the DB is up and running , you need to wait for it using one of these methods or starting your DB first manually with docker-compose up db

Rattler answered 25/10, 2019 at 5:26 Comment(2)
Thanks, that was the problem, I thought that, and added the sleep 10 when starting the web service but that still fails. Waiting for ready for connections from the db service to make the migrations was the solution. ThanksProtium
Can you please help, I have used docker-compose.yml file as yours but getting same error. How did you added "Ready for connection" in docker-compose. Please help. Thank youAbysm
O
3

Make sure that your db docker image is running fine. Check the logs of the running db container. I am able to use MariaDB successfully in my docker-compose file :

db:
image: mariadb
ports:
  - 3306:3306
environment:
  MYSQL_USER: "****"
  MYSQL_PASSWORD: "****"
  MYSQL_DATABASE: "*****"
  MYSQL_RANDOM_ROOT_PASSWORD: "yes"
Outfox answered 24/10, 2019 at 18:31 Comment(1)
thanks, i already did, the container starts fine, creates the database, the user... there seems to be no problem there problem thereProtium
S
0

A bit late for the party but if you are connecting to port 33060 change to port 3306.

Took a whole hour to recognize it exposes two ports.

Splinter answered 11/8, 2021 at 18:42 Comment(0)
H
0

In my case, Dockerfile, docker-compose, database settings, all things were fine still I got error, I got this issue on Windows-10 OS. I restarted the computer everything works fine.

Hotel answered 15/8, 2024 at 13:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.