简体   繁体   中英

Django can't connect to mysql via docker-compose

I have an application using Django and MySQL. The application does not connect to MySQL container from the Django Application but it can be accessed using Sequel Pro with the same credentials.

This is my docker-compose.yml

version: '2'

services:
  db:
    image: mysql:5.7.18
    ports:
      - "3307:3306"
    volumes:
      - "./mysql:/var/lib/mysql"
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=django
      - MYSQL_USER=django
      - MYSQL_PASSWORD=pass

  web:
    build:
      context: .
    depends_on:
      - db
#    links:
#      - db
    volumes:
      - "./app:/src"
    command: bash -c "sleep 3 && python3 src/manage.py runserver 8000"
    ports:
      - "8001:8000"
    environment:
      - DJANGO_SETTINGS_MODULE=app.production
      - DB_NAME=django
      - DB_USER=django
      - DB_PASS=pass
      - DB_HOST=db
      - DB_PORT=3306

This is my django settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.getenv('DB_NAME', 'django'),
        'USER': os.getenv('DB_USER', 'django'),
        'PASSWORD': os.getenv('DB_PASS', 'pass'),
        'HOST': os.getenv('DB_HOST', 'db'),
        'PORT': os.getenv('DB_PORT', '3306'),
        'OPTIONS': {
            'sql_mode': 'STRICT_TRANS_TABLES',
        }
    }
}

And this is error that I don't know how to fix this

web_1    | Performing system checks...
web_1    |
web_1    | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7efeb8563950>
web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1    |     self.connect()
web_1    |   File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/base/base.py", line 189, in connect
web_1    |     self.connection = self.get_new_connection(conn_params)
web_1    |   File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
web_1    |     conn = Database.connect(**conn_params)
web_1    |   File "/usr/local/lib/python3.5/dist-
packages/MySQLdb/__init__.py", line 86, in Connect
web_1    |     return Connection(*args, **kwargs)
web_1    |   File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 204, in __init__
web_1    |     super(Connection, self).__init__(*args, **kwargs2)
web_1    | _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'db' (110)")

Please advice I don't know what I do mistake.

Update: I just see that my container can't send request to sentry.io too

Sentry responded with an error: <urlopen error [Errno -3] Temporary failure in name resolution> (url: https://sentry.io/api/210137/store/)

So This is about docker network? How do I investigate this problem thanks

Try adding the containers to the same network:

version: '2' 
services:
  db: 
    image: mysql:5.7.18
    ...
    networks:
    - base
  web: 
    ...
    networks:
    - base
networks:
  base:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM