在 alpine docker 图像中使用 asyncpg python 模块
Use asyncpg python module in alpine docker image
我正在尝试使用 python 模块 asyncpg
基于 python:3-8.alpine
构建图像。
这是我的 Dockerfile 的一部分:
FROM python:3.8-alpine
RUN apk add gcc
RUN apk add python3-dev
RUN pip3 install asyncpg
我添加了 gcc
和 python3-dev
,因为我认为我需要它们能够根据文档构建 asyncpg
:https://magicstack.github.io/asyncpg/current/installation.html(但我不是可以肯定的是,我认为我应该能够在不构建此模块的情况下进行安装)
但是我在构建镜像的时候出现如下错误:
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/local/include/python3.8 -c asyncpg/pgproto/pgproto.c -o build/temp.linux-x86_64-3.8/asyncpg/pgproto/pgproto.o -O2 -fsigned-char -Wall -Wsign-compare -Wconversion
In file included from asyncpg/pgproto/pgproto.c:29:
/usr/local/include/python3.8/Python.h:11:10: fatal error: limits.h: No such file or directory
11 | #include <limits.h>
| ^~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
如何解决此错误并安装 asyncpg
?
谢谢
您必须安装构建工具,而不仅仅是 gcc。您也可以删除 RUN apk add python3-dev
步骤。由于您使用 python 作为基本图像。
FROM python:3.8-alpine
RUN apk add --no-cache build-base
RUN pip3 install asyncpg
构建标准输出
Step 3/3 : RUN pip3 install asyncpg
---> Running in e109c84feec3
Collecting asyncpg
Downloading asyncpg-0.20.1.tar.gz (734 kB)
Building wheels for collected packages: asyncpg
Building wheel for asyncpg (setup.py): started
Building wheel for asyncpg (setup.py): finished with status 'done'
Created wheel for asyncpg: filename=asyncpg-0.20.1-cp38-cp38-linux_x86_64.whl size=1337371 sha256=5a0dcfc8c327eb4068814a9bf0a1d9beee1b9c440c1100efcede8ef63b1dac09
Stored in directory: /root/.cache/pip/wheels/22/c9/61/991ebd5df043025e222695b7c4bc84e527bf3f0239073f0ea2
Successfully built asyncpg
Installing collected packages: asyncpg
Successfully installed asyncpg-0.20.1
Removing intermediate container e109c84feec3
---> 21dacd97e784
Successfully built 21dacd97e784
您忘记添加 musl-dev
库。
这是工作 Dockerfile
FROM python:3.8-alpine
RUN apk add gcc python3-dev musl-dev
RUN pip3 install asyncpg
我正在尝试使用 python 模块 asyncpg
基于 python:3-8.alpine
构建图像。
这是我的 Dockerfile 的一部分:
FROM python:3.8-alpine
RUN apk add gcc
RUN apk add python3-dev
RUN pip3 install asyncpg
我添加了 gcc
和 python3-dev
,因为我认为我需要它们能够根据文档构建 asyncpg
:https://magicstack.github.io/asyncpg/current/installation.html(但我不是可以肯定的是,我认为我应该能够在不构建此模块的情况下进行安装)
但是我在构建镜像的时候出现如下错误:
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/local/include/python3.8 -c asyncpg/pgproto/pgproto.c -o build/temp.linux-x86_64-3.8/asyncpg/pgproto/pgproto.o -O2 -fsigned-char -Wall -Wsign-compare -Wconversion
In file included from asyncpg/pgproto/pgproto.c:29:
/usr/local/include/python3.8/Python.h:11:10: fatal error: limits.h: No such file or directory
11 | #include <limits.h>
| ^~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
如何解决此错误并安装 asyncpg
?
谢谢
您必须安装构建工具,而不仅仅是 gcc。您也可以删除 RUN apk add python3-dev
步骤。由于您使用 python 作为基本图像。
FROM python:3.8-alpine
RUN apk add --no-cache build-base
RUN pip3 install asyncpg
构建标准输出
Step 3/3 : RUN pip3 install asyncpg
---> Running in e109c84feec3
Collecting asyncpg
Downloading asyncpg-0.20.1.tar.gz (734 kB)
Building wheels for collected packages: asyncpg
Building wheel for asyncpg (setup.py): started
Building wheel for asyncpg (setup.py): finished with status 'done'
Created wheel for asyncpg: filename=asyncpg-0.20.1-cp38-cp38-linux_x86_64.whl size=1337371 sha256=5a0dcfc8c327eb4068814a9bf0a1d9beee1b9c440c1100efcede8ef63b1dac09
Stored in directory: /root/.cache/pip/wheels/22/c9/61/991ebd5df043025e222695b7c4bc84e527bf3f0239073f0ea2
Successfully built asyncpg
Installing collected packages: asyncpg
Successfully installed asyncpg-0.20.1
Removing intermediate container e109c84feec3
---> 21dacd97e784
Successfully built 21dacd97e784
您忘记添加 musl-dev
库。
这是工作 Dockerfile
FROM python:3.8-alpine
RUN apk add gcc python3-dev musl-dev
RUN pip3 install asyncpg