我的应用程序容器无法连接到mysql容器

时间:2019-10-16 11:10:28

标签: python django docker docker-compose dockerfile

在进行了所有的网络搜索并且没有任何答案之后,我想到了在这个平台上问这个问题。我有一个应用程序容器,我尝试与我的数据库容器连接,但是由于原因,我不知道我的应用程序无法连接。 我正在提供所需的所有相关信息。

这是我的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 :(得分:-1)

在settings.py文件中将“ HOST”:“ db”更改为“ HOST”:“ localhost ”。因为您将默认的MySQL端口从容器映射到主主机中的MySQL默认端口。