构建ARM64 Python/Docker镜像时lib-sodium/pynacl编译错误求助
问题概述
为在MacBook M1(Pro)本地更高效运行项目,尝试构建ARM64镜像替代运行缓慢的x64镜像(启动、HTTP响应、测试耗时过长),但构建过程中PyNaCl安装失败,导致镜像无法正常生成。
Dockerfile内容
FROM arm64v8/python # Install system dependencies RUN apt-get update && apt-get install -y \ git \ curl \ libpq-dev \ build-essential \ libssl-dev \ libffi-dev # Install Node.js for ARM64 RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - RUN apt-get install -y nodejs # Set the working directory in the container WORKDIR /app # Install Python dependencies RUN pip install --no-cache-dir cryptography RUN pip install --no-cache-dir pyNaCl==1.3.0 # Copy the requirements.txt file to the container COPY requirements.txt . # Install additional Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy the Django project files to the container COPY . . # Expose the Django development server port EXPOSE 8000 # Start the Django development server CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
requirements.txt内容
autopep8==1.6.0 kombu>=5.0.0 # celery dependency celery==5.0.0 dj-database-url==0.5 django==3.2.13 django-bootstrap4==2.2.0 django-cors-headers==3.4.0 django-debug-toolbar==3.2.1 django-phonenumber-field==4.0.0 django-safedelete==0.5.6 djangorestframework==3.13.0 djangorestframework-xml==2.0.0 django-model-utils==4.2.0 gunicorn==20.0.4 ipdb==0.13.3 jedi==0.18.1 # ipython dependency ipython==7.32.0 ipython-genutils==0.2.0 newrelic==7.16.0.178 pep8==1.7.1 python-dateutil>=2.8.0 pytz>=2019.2 redis==3.5.3 django-redis==4.12.1 requests==2.27.1 requests-mock>=1.6.0 selenium==3.141.0 twilio==7.8.0 django-filter==2.4.0 psycopg2==2.9.3 django-session-security==2.6.6 django-axes==5.4.1 beautifulsoup4==4.9.1 git+https://github.com/onitsoft/uphold-sdk-python.git#egg=uphold isort==5.2.0 freezegun>=0.3.1 suds-community>=1.1.0 drf-extensions>=0.5.0 requests-cache==0.5.2 xmltodict==0.12 python-bitcoinrpc==1.0 urllib3==1.26.5 pillow==9.1.0 git+https://github.com/onitsoft/django-loginurl.git#egg=django-loginurl django-countries==7.3.2 tornado==6.0.4 # flower dependency flower==0.9.5 django-guardian==2.4.0 dredd-hooks==0.2.0 django-fsm==2.8.0 git+https://github.com/onitsoft/python-bittrex.git#egg=python-bittrex cached-property==1.5.1 web3==5.25.0 # pycrypto==2.6.1 pycryptodome==3.14.1 django-extensions==3.1.5 django-oauth-toolkit==1.7.1 iptools==0.7.0 django-otp==0.9.3 qrcode==6.1 git+https://github.com/onitsoft/django-audit-log.git@django3-support rlp==1.2.0 django-newsletter==0.9.1 git+https://github.com/onitsoft/py-stellar-base.git@2.10.0 django-email-log==1.2.0 django-nested-admin==3.4.0 #bintrees==2.0.7 sortedcontainers==2.2.2 git+https://github.com/onitsoft/OrderBook.git@5dbd18b4534ac4945b7e53315c65e1d49d9e340d#egg=orderbook django-picklefield===2.1.1 pdbpp==0.10.2 pyyaml==5.4.1 git+https://github.com/onitsoft/ripple-python django-constance==2.9.1 django-admin-rangefilter==0.8.4 toml==0.10.1 phonenumbers==8.12.7 django-post-office==3.4.1 slackclient==2.7.3 django-intercom==0.1.3 django-ipware==3.0.7 django-enum-choices==2.1.3 git+https://github.com/onitsoft/monero-python.git@v0.9.3 simplejson==3.17.2 django-polymorphic==3.1.0 retrying==1.3.3 ccxt==3.0.51 django-admin-multiple-choice-list-filter==0.1.1 celery-once==3.0.1 bidict==0.21.2 fireblocks-sdk==1.19.0 pycountry==20.7.3 cbor==1.0.0 git+https://github.com/onitsoft/django-admin-comments.git lxml==4.9.1 supervisor==4.2.4 django-admin-csvexport==1.11 rsa==4.9 pydantic==1.10.2 XlsxWriter==3.0.8
报错信息
编译PyNaCl时出现依赖缺失或编译失败,核心问题为无法正确链接或编译libsodium相关组件,完整错误栈内容过长。
已尝试的解决方案
- 通过apt安装libsodium而非依赖pip编译
- 指定安装pyNaCl==1.3.0版本
- 在MacOS主机本地通过
brew install libsodium安装libsodium
以上方案均未解决问题。
可行解决方案
1. 补充系统依赖中的libsodium开发包
原Dockerfile未安装libsodium的开发头文件和库,导致PyNaCl编译时找不到依赖。修改系统依赖安装步骤,添加libsodium-dev:
RUN apt-get update && apt-get install -y \ git \ curl \ libpq-dev \ build-essential \ libssl-dev \ libffi-dev \ libsodium-dev
2. 强制PyNaCl使用系统安装的libsodium
设置环境变量SODIUM_INSTALL=system,让PyNaCl跳过本地编译libsodium,直接使用系统已安装的版本:
ENV SODIUM_INSTALL=system RUN pip install --no-cache-dir pyNaCl==1.3.0
3. 升级PyNaCl到兼容ARM64的版本
PyNaCl 1.3.0对ARM64的支持存在缺陷,升级到1.5.0及以上版本可解决编译问题。修改Dockerfile中的安装命令:
RUN pip install --no-cache-dir pyNaCl==1.5.0
同时确保requirements.txt中对应的版本号同步更新,避免版本冲突。
完整修改后的Dockerfile
FROM arm64v8/python # Install system dependencies including libsodium-dev RUN apt-get update && apt-get install -y \ git \ curl \ libpq-dev \ build-essential \ libssl-dev \ libffi-dev \ libsodium-dev # Install Node.js for ARM64 RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - RUN apt-get install -y nodejs # Force PyNaCl to use system-installed libsodium ENV SODIUM_INSTALL=system WORKDIR /app # Install Python dependencies RUN pip install --no-cache-dir cryptography RUN pip install --no-cache-dir pyNaCl==1.5.0 COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8000 CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
额外操作提示
- 构建镜像时添加
--no-cache参数,确保所有依赖重新安装:docker build --no-cache -t your-image-tag . - 检查requirements.txt中是否存在重复的PyNaCl版本声明,如有则统一为升级后的版本。
内容的提问来源于stack exchange,提问作者Oleg Belousov
相关产品推荐
相关产品推荐

