Мой контейнер приложения не может работать с моим MySQL контейнером - PullRequest
0 голосов
/ 16 октября 2019

После всех поисков в сети и появления ответа на этот вопрос я подумал задать этот вопрос на этой платформе. У меня был контейнер приложения, который я пытаюсь соединить с моим контейнером базы данных, но по неизвестным причинам мое приложение не может подключиться. Я предоставляю всю необходимую информацию для этого.

Вот мой файл docker-compose

version: '2'

services:

  application: &application
    image: gmasmatrix_application:latest
    command: /start.sh
    volumes:
      - .:/app
    depends_on:
      - db
    ports:
    - 8000:8000
#    cpu_shares: 874
#    mem_limit: 1610612736
#    mem_reservation: 1610612736
    build:
      context: ./
      dockerfile: ./compose/local/application/Dockerfile
      args:
      - GMAS_ENV_TYPE=local

  db:
    image: mysql:5.7.10
    environment:
      MYSQL_DATABASE: gmas_mkt
      MYSQL_ROOT_PASSWORD: pulkit1607
    ports:
    - '3306:3306'
    volumes:
      - my-db:/var/lib/mysql


volumes:
  my-db:

Я также изменил хост в файле settings.py

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

Вот ошибка, которую я получаю при запуске файла docker-compose up

application_1  | Traceback (most recent call last):
application_1  |   File "manage.py", line 22, in <module>
application_1  |     execute_from_command_line(sys.argv)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
application_1  |     utility.execute()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
application_1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
application_1  |     self.execute(*args, **cmd_options)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
application_1  |     self.check()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 482, in check
application_1  |     include_deployment_checks=include_deployment_checks,
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
application_1  |     new_errors = check(app_configs=app_configs)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models
application_1  |     errors.extend(model.check(**kwargs))
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 1205, in check
application_1  |     errors.extend(cls._check_fields(**kwargs))
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 1282, in _check_fields
application_1  |     errors.extend(field.check(**kwargs))
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 934, in check
application_1  |     errors = super(AutoField, self).check(**kwargs)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 207, in check
application_1  |     errors.extend(self._check_backend_specific_checks(**kwargs))
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 306, in _check_backend_specific_checks
application_1  |     return connection.validation.check_field(self, **kwargs)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 18, in check_field
application_1  |     field_type = field.db_type(connection)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 614, in db_type
application_1  |     return connection.data_types[self.get_internal_type()] % data
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
application_1  |     return getattr(connections[DEFAULT_DB_ALIAS], item)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 59, in __get__
application_1  |     res = instance.__dict__[self.name] = self.func(instance)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 196, in data_types
application_1  |     if self.features.supports_microsecond_precision:
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 59, in __get__
application_1  |     res = instance.__dict__[self.name] = self.func(instance)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/features.py", line 52, in supports_microsecond_precision
application_1  |     return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 59, in __get__
application_1  |     res = instance.__dict__[self.name] = self.func(instance)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 371, in mysql_version
application_1  |     with self.temporary_connection():
application_1  |   File "/usr/local/lib/python2.7/contextlib.py", line 17, in __enter__
application_1  |     return self.gen.next()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 462, in temporary_connection
application_1  |     cursor = self.cursor()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
application_1  |     cursor = self.make_debug_cursor(self._cursor())
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
application_1  |     self.ensure_connection()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
application_1  |     self.connect()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
application_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
application_1  |     self.connect()
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
application_1  |     self.connection = self.get_new_connection(conn_params)
application_1  |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 276, in get_new_connection
application_1  |     conn = Database.connect(**conn_params)
application_1  |   File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
application_1  |     return Connection(*args, **kwargs)
application_1  |   File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 166, in __init__
application_1  |     super(Connection, self).__init__(*args, **kwargs2)
application_1  | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")

Невозможно понять, что я делаю неправильно.

1 Ответ

0 голосов
/ 16 октября 2019

Измените 'HOST': 'db' на 'HOST': ' localhost ' в файле settings.py. Потому что вы сопоставляете порт MySQL по умолчанию из контейнера с портом MySQL по умолчанию на вашем главном хосте.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...