フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

Dockerのpython:alpine環境でmysqlclientをインストールするとエラーが出る

はじめに

  • Dockerを利用してpythonのfastapiの環境を作るために環境構築していたところ幾つかのエラーにあったので、対処法を下記に記載する

エラー①

  • mariadb-devをインストールすることで解決する
Collecting mysqlclient
  Downloading mysqlclient-2.0.1.tar.gz (87 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-by0mw_7_/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-by0mw_7_/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-on_1rq_n
         cwd: /tmp/pip-install-by0mw_7_/mysqlclient/
    Complete output (12 lines):
    /bin/sh: mysql_config: not found
    /bin/sh: mariadb_config: not found
    /bin/sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-by0mw_7_/mysqlclient/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-by0mw_7_/mysqlclient/setup_posix.py", line 65, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-by0mw_7_/mysqlclient/setup_posix.py", line 31, in mysql_config
        raise OSError("{} not found".format(_mysql_config_path))
    OSError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Service 'fastapi' failed to build: The command '/bin/sh -c pip install --trusted-host pypi.python.org -r requirements.txt' returned a non-zero code: 1

エラー②

  • gccをインストールすることで解決する
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.9
    creating build/temp.linux-x86_64-3.9/MySQLdb
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -Dversion_info=(2,0,1,'final',0) -D__version__=2.0.1 -I/usr/include/mysql -I/usr/include/mysql/mysql -I/usr/local/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.9/MySQLdb/_mysql.o
    error: command 'gcc' failed: No such file or directory

エラー③

  • libc-devをインストールすることで解決できる
    creating build/temp.linux-x86_64-3.9/MySQLdb
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -Dversion_info=(2,0,1,'final',0) -D__version__=2.0.1 -I/usr/include/mysql -I/usr/include/mysql/mysql -I/usr/local/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.9/MySQLdb/_mysql.o
    In file included from MySQLdb/_mysql.c:29:
    /usr/include/mysql/mysql.h:38:10: fatal error: sys/types.h: No such file or directory
       38 | #include <sys/types.h>
          |          ^~~~~~~~~~~~~
    compilation terminated.
    error: command '/usr/bin/gcc' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s40h57jn/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s40h57jn/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-kzqpkfu5/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/mysqlclient Check the logs for full command output.
ERROR: Service 'fastapi' failed to build: The command '/bin/sh -c pip install --trusted-host pypi.python.org -r requirements.txt' returned a non-zero code: 1

Dockerfile

  • 最終的には下記のファイルでbuildすることができた。
FROM python:3.9-alpine

RUN apk add --no-cache mariadb-dev  gcc libc-dev

WORKDIR /srv/api/src

ADD requirements.txt .

RUN pip install --trusted-host pypi.python.org -r requirements.txt
  • requirements.txtの内容
uvicorn
fastapi
mysqlclient
sqlalchemy

参考にしたページはこちら

github.com