我有Django
应用程序,可与远程MySQL
数据库一起使用。我在Docker容器的帮助下运行应用程序。不幸的是,在应用程序日志中,我注意到了错误。当我在没有Docker的情况下使用我的应用程序时,它可以工作。为什么在Docker中运行的应用程序无法连接到数据库?
ERROR Internal Server Error: /main/login/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/beewifi/main/views/auth.py", line 47, in wifi_login
zone = Zone.objects.get(identity=params['identity'])
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
num = len(clone)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1105, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 863, in execute_sql
sql, params = self.as_sql()
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 420, in as_sql
where, w_params = self.compile(self.where) if self.where is not None else ("", [])
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 373, in compile
sql, params = node.as_sql(self, self.connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 79, in as_sql
sql, params = compiler.compile(child)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 373, in compile
sql, params = node.as_sql(self, self.connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 169, in as_sql
lhs_sql, params = self.process_lhs(compiler, connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 162, in process_lhs
db_type = self.lhs.output_field.db_type(connection=connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 644, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 174, in data_types
if self.features.supports_microsecond_precision:
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 385, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/local/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 591, in temporary_connection
cursor = self.cursor()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on '172.28.5.17' (110)")
Dockerfile:
FROM python:2.7.14-jessie
RUN mkdir /db && mkdir /django_logs && mkdir /static && mkdir /photo
ADD server ./server
WORKDIR /my_app
COPY ./requirements.txt /my_app/requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt --timeout 1000 --no-cache-dir
COPY . /my_app
WORKDIR /
RUN bash my_app/nodesource_setup.sh
RUN apt-get install -y nodejs
WORKDIR /my_app/static
RUN npm install
WORKDIR /my_app
CMD bash run.sh
run.sh:
python manage.py collectstatic --noinput
python manage.py migrate --noinput
gunicorn --bind 0.0.0.0:8001 --workers 3 --timeout 3600 --worker-class gevent --log-level=DEBUG --access-logfile - --error-logfile - --user 0 --group 0 smswifi.wsgi
settings.py:
***
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'XXXX',
'USER': 'XXXX',
'PASSWORD': 'XXX',
'HOST': 'XXX.XX.X.XX',
'PORT': '3306',
}
}
***
答案 0 :(得分:-1)
由于docker像虚拟机一样运行,因此它可能正在内部寻找IP地址。如果您检查数据库服务器日志并发现没有连接被拒绝或导致错误,则可能需要将docker应用配置为使用主机IP