Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/govtcareer_api
COPY ./ /usr/src/govtcareer_api
RUN pip install -r requirements.txt
CMD ["/bin/bash"]
docker-compose.yml
version: "3"
services:
govtcareer_api:
container_name: govtcareer
build: .
command: "bash -c 'python manage.py migrate --no-input && python manage.py runserver 0.0.0.0:8000'"
working_dir: /usr/src/govtcareer_api
ports:
- "8000:8000"
volumes:
- ./:/usr/src/govtcareer_api
links:
- db
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=Thinkonce
- MYSQL_USER=soubhagya
- MYSQL_PASSWORD=Thinkonce
- MYSQL_DATABASE=freejobalert
ports:
- "3306:3306"
django数据库连接:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'freejobalert',
'USER': 'soubhagya',
'PASSWORD': 'Thinkonce',
'HOST': 'localhost',
'PORT': '3306',
},
}
错误:
govtcareer | include_deployment_checks=include_deployment_checks,
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
govtcareer | issues = run_checks(tags=[Tags.database])
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
govtcareer | new_errors = check(app_configs=app_configs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
govtcareer | issues.extend(conn.validation.check(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
govtcareer | issues.extend(self._check_sql_mode(**kwargs))
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
govtcareer | with self.connection.cursor() as cursor:
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
govtcareer | return self._cursor()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
govtcareer | self.ensure_connection()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
govtcareer | raise dj_exc_value.with_traceback(traceback) from exc_value
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
govtcareer | self.connect()
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
govtcareer | self.connection = self.get_new_connection(conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
govtcareer | return Database.connect(**conn_params)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
govtcareer | return Connection(*args, **kwargs)
govtcareer | File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
govtcareer | super(Connection, self).__init__(*args, **kwargs2)
govtcareer | django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")')
这是我的代码。
我正在尝试将django与mysql集成到docker容器中 但显示连接被拒绝托管。
我需要在docker内部使用环境变量吗?
我用于mongodb的方式相同,但是在mysql中,我正面临这个问题。
请看一下我的代码。
答案 0 :(得分:1)
从您的links
中删除docker-compose.yml
。
links:
- db
您不需要它们-泊坞窗将构建一个网络并将所有已声明的服务包括在其中。