1

I built 2 containers, one for project with python-3.4.10 and another for MySQL (with mysql-5.7-debian).

When I try to run any command for example: python manage.py makemigrations {name} or python manage.py runserver 0.0.0.0:8000 it gives me the following error:

django.db.utils.OperationalError: (2003, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')

I have literally tried everything and went through each stack issues related to it. But the issue still persists.

When I connect on MySQL Workbench (which is installed on my base OS: Windows 11) with the running MySQL container, it gets connected but when I run the command of python manage.py ... on the different container it shows me the above error.

Could you please help me out here? Thank you.

settings.py

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

docker-compose.yaml

version: '3.8'

services:
  web:
    image: s-compose:latest
    ports:
      - 8000:8000
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"
    volumes:
      - ./s:/home/app/webapp
    depends_on:
      - database

  database:
    image: mysql:5.7-debian
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: root

.Dockerfile

FROM python:3.4.10

ENV Dockerhome=/home/app/webapp
RUN mkdir -p $Dockerhome

WORKDIR $Dockerhome

COPY ./sansthaonline $Dockerhome

RUN pip install -r req.txt; exit 0

EXPOSE 8000
## testing server
CMD python manage.py runserver 0.0.0.0:8000
Remus Case
  • 460
  • 4
  • 12
  • 2
    I assume it's because of `'HOST': '127.0.0.1'`. You should use container's name, e.g. `'HOST': 'database'`, since you define in your Compose file that name of the DB container is `database`. – Stefan Golubović Jul 01 '22 at 14:46

0 Answers0