使用Docker中的mysqlclient

huangapple go评论61阅读模式
英文:

using mysqlclient in docker

问题

我在努力安装mysqlclient在Docker上。

我在互联网上看到了很多不同的建议,比如:

  1. 运行 apt-get install -y libmysqlclient-dev
  2. 运行 apt-get update && apt-get install -y default-libmysqlclient-dev
  3. 运行 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....

  1. RUN apt-get install -y libmysqlclient-dev
  2. RUN apt-get update && apt-get install -y default-libmysqlclient-dev
  3. 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 &amp;&amp; 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 [&quot;python&quot;, &quot;manage.py&quot;, &quot;runserver&quot;, &quot;0.0.0.0:8001&quot;]

output from attempt 1

 =&gt; ERROR [3/7] RUN apt-get install -y libmysqlclient-dev                                                          1.1s
------
 &gt; [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 &#39;libmysqlclient-dev&#39; 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&gt;docker logs frontend
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.9/threading.py&quot;, line 980, in _bootstrap_inner
    self.run()
  File &quot;/usr/local/lib/python3.9/threading.py&quot;, line 917, in run
    self._target(*self._args, **self._kwargs)
  File &quot;/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py&quot;, line 64, in wrapper
    fn(*args, **kwargs)
  File &quot;/usr/local/lib/python3.9/site-packages/daphne/management/commands/runserver.py&quot;, line 96, in inner_run
    self.check(display_num_errors=True)
  File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/base.py&quot;, line 485, in check
    all_issues = checks.run_checks(
  File &quot;/usr/local/lib/python3.9/site-packages/django/core/checks/registry.py&quot;, line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File &quot;/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py&quot;, line 14, in check_url_config
    return check_resolver(resolver)
  File &quot;/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py&quot;, line 24, in check_resolver
    return check_method()
  File &quot;/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py&quot;, line 494, in check
    for pattern in self.url_patterns:
  File &quot;/usr/local/lib/python3.9/site-packages/django/utils/functional.py&quot;, line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File &quot;/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py&quot;, line 715, in url_patterns
    patterns = getattr(self.urlconf_module, &quot;urlpatterns&quot;, self.urlconf_module)
  File &quot;/usr/local/lib/python3.9/site-packages/django/utils/functional.py&quot;, line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File &quot;/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py&quot;, line 708, in urlconf_module
    return import_module(self.urlconf_name)
  File &quot;/usr/local/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1030, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1007, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 986, in _find_and_load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 680, in _load_unlocked
  File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 850, in exec_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 228, in _call_with_frames_removed
  File &quot;/frontend/frontend/urls.py&quot;, line 21, in &lt;module&gt;
    path(&#39;&#39;, include(&#39;chat.urls&#39;))
  File &quot;/usr/local/lib/python3.9/site-packages/django/urls/conf.py&quot;, line 38, in include
    urlconf_module = import_module(urlconf_module)
  File &quot;/usr/local/lib/python3.9/importlib/__init__.py&quot;, line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1030, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1007, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 986, in _find_and_load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 680, in _load_unlocked
  File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 850, in exec_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 228, in _call_with_frames_removed
  File &quot;/frontend/chat/urls.py&quot;, line 18, in &lt;module&gt;
    from . import views
  File &quot;/frontend/chat/views.py&quot;, line 12, in &lt;module&gt;
    import mysqlclient
ModuleNotFoundError: No module named &#39;mysqlclient&#39;

attempt 3 output

C:\Users\jack.flavell\Desktop\New folder (22)\big_ui_system&gt;docker-compose -f docker-compose-testing.yml build frontend
[+] Building 0.3s (2/3)
 =&gt; [internal] load build definition from Dockerfile.local                                                         0.0s
[+] Building 1.8s (6/10)
 =&gt; [internal] load build definition from Dockerfile.local                                                         0.0s
 =&gt; =&gt; transferring dockerfile: 499B                                                                               0.0s
 =&gt; [internal] load .dockerignore                                                                                  0.0s
 =&gt; =&gt; transferring context: 2B                                                                                    0.0so
 =&gt; [internal] load metadata for docker.io/library/python:3.9                                                      0.8s
 =&gt; CACHED [1/6] FROM docker.io/library/python:3.9@sha256:98f018a1afd67f2e17a4abd5bfe09b998734ba7c1ee54780e7ed216  0.0s
 =&gt; [internal] load build context                                                                                  0.0s
 =&gt; =&gt; transferring context: 1.90kB                                                                                0.0s
 =&gt; ERROR [2/6] RUN apt-get install mysql-community-client                                                         0.8s
------
 &gt; [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&gt;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 &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.

&gt;&gt;&gt; import mysqlclient

Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ModuleNotFoundError: No module named &#39;mysqlclient&#39;

&gt;&gt;&gt; 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 &#39;root&#39; 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

huangapple
  • 本文由 发表于 2023年6月22日 20:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定