skip to Main Content

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 && 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:Usersjack.flavellDesktopNew 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:Usersjack.flavellDesktopNew 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:Usersjack.flavellDesktopNew 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?

2

Answers


  1. As I understand, the issue is with the installation of libmysqlclient-dev!

    first of all, write apt-get update before installing libmysqlclient-dev then try to use default-libmysqlclient-dev instead of libmysqlclient-dev and at the end use mysql-client instead of mysql-community-client!

    when it’s done,try to rebuild your Docker image

    good luck!

    Login or Signup to reply.
  2. Maybe the pip install is missing some other packages.
    Have you tried the command from the mysqlclient-page?
    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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search