英文:
using mysqlclient in docker
问题
我在努力安装mysqlclient在Docker上。
我在互联网上看到了很多不同的建议,比如:
- 运行 apt-get install -y libmysqlclient-dev
- 运行 apt-get update && apt-get install -y default-libmysqlclient-dev
- 运行 apt-get install mysql-community-client
我都尝试过了。
以下是我的Dockerfile(我分别尝试了1、2和3):
FROM python:3.9
RUN apt-get install -y libmysqlclient-dev ## 尝试1
RUN apt-get update && apt-get install -y default-libmysqlclient-dev # 尝试2
RUN apt-get install mysql-community-client ## 尝试3
RUN pip install asgiref boto3 botocore Django Jinja2 jmespath MarkupSafe numpy pandas python-dateutil pytz PyYAML render s3transfer six sqlparse tzdata urllib3 asyncio websockets channels mysqlclient mysql-connector-python bcrypt python-dotenv cryptography mysql
RUN pip install daphne
COPY . /frontend
WORKDIR /frontend
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]
尝试1的输出:
ERROR [3/7] RUN apt-get install -y libmysqlclient-dev 1.1s
------
[3/7] RUN apt-get install -y libmysqlclient-dev:
#0 0.417 Reading package lists...
#0 0.947 Building dependency tree...
#0 1.076 Reading state information...
#0 1.083 Package libmysqlclient-dev is not available, but is referred to by another package.
#0 1.083 This may mean that the package is missing, has been obsoleted, or
#0 1.083 is only available from another source
#0 1.083 However the following packages replace it:
#0 1.083 libmariadb-dev-compat libmariadb-dev
#0 1.083
#0 1.091 E: Package 'libmysqlclient-dev' has no installation candidate
------
failed to solve: executor failed running [/bin/sh -c apt-get install -y libmysqlclient-dev]: exit code: 100
尝试2在构建时成功,但在启动时失败。
尝试3的输出:
C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system>docker-compose -f docker-compose-testing.yml build frontend
[+] Building 0.3s (2/3)
=> [internal] load build definition from Dockerfile.local 0.0s
[+] Building 1.8s (6/10)
=> [internal] load build definition from Dockerfile.local 0.0s
=> => transferring dockerfile: 499B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0so
=> [internal] load metadata for docker.io/library/python:3.9 0.8s
=> CACHED [1/6] FROM docker.io/library/python:3.9@sha256:98f018a1afd67f2e17a4abd5bfe09b998734ba7c1ee54780e7ed216 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.90kB 0.0s
=> ERROR [2/6] RUN apt-get install mysql-community-client 0.8s
------
[2/6] RUN apt-get install mysql-community-client:
#0 0.783 Reading package lists...
#0 0.805 Building dependency tree...
#0 0.807 Reading state information...
#0 0.809 E: Unable to locate package mysql-community-client
------
failed to solve: executor failed running [/bin/sh -c apt-get install mysql-community-client]: exit code: 100
因此,由于以上方法都没有成功,我还尝试进入Docker容器手动安装它。
C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system>docker exec -it 324018499d5b bash
root@frontend:/frontend# python
Python 3.9.17 (main, Jun 14 2023, 18:56:13)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits", or "license" for more information.
>>> import mysqlclient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mysqlclient'
>>> exit()
root@frontend:/frontend# pip install mysqlclient
Requirement already satisfied: mysqlclient in /usr/local/lib/python3.9/site-packages (2.2.0)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
它无法导入到Python中,但声称已经安装了。请问我该如何在Docker中让mysqlclient正常工作?
英文:
im struggling to install mysqlclient on docker
ive seen a whole bunch of different suggestions on the internet like....
- RUN apt-get install -y libmysqlclient-dev
- RUN apt-get update && apt-get install -y default-libmysqlclient-dev
- RUN apt-get install mysql-community-client
and ive tried them all
below is my dockerfile (ive tried attempt 1, 2 and 3 seperatley)
FROM python:3.9
RUN apt-get install -y libmysqlclient-dev ## attempt 1
RUN apt-get update && apt-get install -y default-libmysqlclient-dev # attempt 2
RUN apt-get install mysql-community-client ## attempt 3
RUN pip install asgiref boto3 botocore Django Jinja2 jmespath MarkupSafe numpy pandas python-dateutil pytz PyYAML render s3transfer six sqlparse tzdata urllib3 asyncio websockets channels mysqlclient mysql-connector-python bcrypt python-dotenv cryptography mysql
RUN pip install daphne
COPY . /frontend
WORKDIR /frontend
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]
output from attempt 1
=> ERROR [3/7] RUN apt-get install -y libmysqlclient-dev 1.1s
------
> [3/7] RUN apt-get install -y libmysqlclient-dev:
#0 0.417 Reading package lists...
#0 0.947 Building dependency tree...
#0 1.076 Reading state information...
#0 1.083 Package libmysqlclient-dev is not available, but is referred to by another package.
#0 1.083 This may mean that the package is missing, has been obsoleted, or
#0 1.083 is only available from another source
#0 1.083 However the following packages replace it:
#0 1.083 libmariadb-dev-compat libmariadb-dev
#0 1.083
#0 1.091 E: Package 'libmysqlclient-dev' has no installation candidate
------
failed to solve: executor failed running [/bin/sh -c apt-get install -y libmysqlclient-dev]: exit code: 100
attempt 2 builds, but fails when starting
C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system>docker logs frontend
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/threading.py", line 980, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.9/threading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/daphne/management/commands/runserver.py", line 96, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 485, in check
all_issues = checks.run_checks(
File "/usr/local/lib/python3.9/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py", line 24, in check_resolver
return check_method()
File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 494, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 715, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/frontend/frontend/urls.py", line 21, in <module>
path('', include('chat.urls'))
File "/usr/local/lib/python3.9/site-packages/django/urls/conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/frontend/chat/urls.py", line 18, in <module>
from . import views
File "/frontend/chat/views.py", line 12, in <module>
import mysqlclient
ModuleNotFoundError: No module named 'mysqlclient'
attempt 3 output
C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system>docker-compose -f docker-compose-testing.yml build frontend
[+] Building 0.3s (2/3)
=> [internal] load build definition from Dockerfile.local 0.0s
[+] Building 1.8s (6/10)
=> [internal] load build definition from Dockerfile.local 0.0s
=> => transferring dockerfile: 499B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0so
=> [internal] load metadata for docker.io/library/python:3.9 0.8s
=> CACHED [1/6] FROM docker.io/library/python:3.9@sha256:98f018a1afd67f2e17a4abd5bfe09b998734ba7c1ee54780e7ed216 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.90kB 0.0s
=> ERROR [2/6] RUN apt-get install mysql-community-client 0.8s
------
> [2/6] RUN apt-get install mysql-community-client:
#0 0.783 Reading package lists...
#0 0.805 Building dependency tree...
#0 0.807 Reading state information...
#0 0.809 E: Unable to locate package mysql-community-client
------
failed to solve: executor failed running [/bin/sh -c apt-get install mysql-community-client]: exit code: 100
so since none of that worked ive also tried going into my docker container to install it manually
C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system>docker exec -it 324018499d5b bash
root@frontend:/frontend# python
Python 3.9.17 (main, Jun 14 2023, 18:56:13)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysqlclient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mysqlclient'
>>> exit()
root@frontend:/frontend# pip install mysqlclient
Requirement already satisfied: mysqlclient in /usr/local/lib/python3.9/site-packages (2.2.0)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
IT WONT IMPORT INTO PYTHON BUT CLAIMS ITS ALLREADY INSTALLED >:( AAAAARRRRGGGHH
please help, how can i get mysqlclient to work in docker?
答案1
得分: 2
可能pip install
缺少其他一些包。<br>
您尝试过从mysqlclient页面运行以下命令吗?<br> sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
在您的Dockerfile中尝试:
FROM python:3.9
RUN apt-get update -y
RUN apt-get install -y python3-dev build-essential
RUN apt-get install -y default-libmysqlclient-dev
英文:
Maybe the pip install
is missing some other packages.<br>
Have you tried the command from the mysqlclient-page?<br> sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
In your Dockerfile try:
FROM python:3.9
RUN apt-get update -y
RUN apt-get install -y python3-dev build-essential
RUN apt-get install -y default-libmysqlclient-dev
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论