🧱

psycopg2をコンテナ内で使おうとしたらエラーが出る問題の修正

2021/03/28に公開

psycopg2 をコンテナ内で使おうとした。

Dockerfile

FROM python:3.9-alpine
RUN pip psycopg2
COPY server/ /app
WORKDIR /app
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--reload"]

滅茶苦茶エラーがでてコケる。

...
#5 27.70          cwd: /tmp/pip-install-21mivfpi/psycopg2_58a0d72126304cb7a3acd8bc31f89aea/
#5 27.70     Complete output (6 lines):
#5 27.70     Traceback (most recent call last):
#5 27.70       File "<string>", line 1, in <module>
#5 27.70       File "/tmp/pip-install-21mivfpi/psycopg2_58a0d72126304cb7a3acd8bc31f89aea/setup.py", line 225
#5 27.70         except Warning, w:
#5 27.70                       ^
#5 27.70     SyntaxError: invalid syntax
#5 27.70     ----------------------------------------
#5 27.70 WARNING: Discarding https://files.pythonhosted.org/packages/2d/d7/496da11d7c81971870ddd36800419c4f84e8f6208aac5eabedf9f7748729/psycopg2-2.0.11.tar.gz#sha256=e6b4e0e41df97441eff34e00065376414da6488e0d55848a45cd77551dbae434 (from https://pypi.org/simple/psycopg2/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#5 27.80   Downloading psycopg2-2.0.10.tar.gz (255 kB)
#5 28.03     ERROR: Command errored out with exit status 1:
#5 28.03      command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-21mivfpi/psycopg2_a044cd11806a40beafbfe246d89c07b9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-21mivfpi/psycopg2_a044cd11806a40beafbfe246d89c07b9/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-2myiqp_6
#5 28.03          cwd: /tmp/pip-install-21mivfpi/psycopg2_a044cd11806a40beafbfe246d89c07b9/
#5 28.03     Complete output (5 lines):
#5 28.03     Traceback (most recent call last):
#5 28.03       File "<string>", line 1, in <module>
#5 28.03       File "/tmp/pip-install-21mivfpi/psycopg2_a044cd11806a40beafbfe246d89c07b9/setup.py", line 50, in <module>
#5 28.03         import ConfigParser
#5 28.03     ModuleNotFoundError: No module named 'ConfigParser'
#5 28.03     ----------------------------------------
#5 28.03 WARNING: Discarding https://files.pythonhosted.org/packages/19/79/35c7596bab4456f3610c12ec542a94d51c6781ced587d1d85127210b879b/psycopg2-2.0.10.tar.gz#sha256=e40cc04b43849085725076ae134bfef9e3b087f6dd7c964aeeb930e2f0bc14ab (from https://pypi.org/simple/psycopg2/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#5 28.03 ERROR: Could not find a version that satisfies the requirement psycopg2
#5 28.03 ERROR: No matching distribution found for psycopg2
------
executor failed running [/bin/sh -c pip install psycopg2]: exit code: 1
ERROR: Service 'app' failed to build
make: *** [run-compose] Error 1

修正方法

以下の処理をdockefileに加える。

RUN apk --no-cache add build-base
RUN apk --no-cache add postgresql-dev
RUN python3 -m pip install psycopg2

参考

ここに書いてあった。
https://github.com/Docker-Hub-frolvlad/docker-alpine-python3/issues/1

最初に到達したサイト。ココから上のissueにたどり着いた。
https://blog.ikappio.com/install-of-psycopg2-fails-with-python-3-7-alpine-3-8/

Discussion