Docker centos7 镜像:psutil 在 Windows/Linux 上安装但在 Mac 上失败

问题描述 投票:0回答:3

这个 Dockerfile 在 Windows/Linux 上运行良好

FROM centos:centos7
RUN yum -y install python3 gcc
RUN pip3 install --upgrade pip
RUN pip3 install psutil

我通过做来塑造形象

docker build -t anytag .

但是同一个 Dockerfile 在 MacOS 上失败。我在两台不同的 Mac(M1 芯片)上尝试过

我得到的错误是

#0 93.73   gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=591 -DPSUTIL_LINUX=1 -I/usr/include/python3.6m -c psutil/_psutil_common.c -o build/temp.linux-aarch64-3.6/psutil/_psutil_common.o
#0 93.73   psutil/_psutil_common.c:9:20: fatal error: Python.h: No such file or directory
#0 93.73    #include <Python.h>
#0 93.73                       ^
#0 93.73   compilation terminated.
#0 93.73   error: command 'gcc' failed with exit status 1
#0 93.73   ----------------------------------------
#0 93.73   ERROR: Failed building wheel for psutil
#0 93.73   Building wheel for pendulum (pyproject.toml): started
#0 94.04   Building wheel for pendulum (pyproject.toml): finished with status 'done'
#0 94.04   Created wheel for pendulum: filename=pendulum-2.1.2-cp36-cp36m-manylinux_2_17_aarch64.whl size=109769 sha256=2373b525a3ca7c5991eb07b0fb7fd31663a114e998480b9d4e09fe658f154152
#0 94.04   Stored in directory: /home/etl/.cache/pip/wheels/c7/33/5b/9bd231ee982125a3e4eaa5c77a04de43eba25ee940cfb5a84a
#0 94.04 Successfully built pendulum
#0 94.04 Failed to build psutil
#0 94.04 ERROR: Could not build wheels for psutil, which is required to install pyproject.toml-based projects
------
failed to solve: executor failed running [/bin/sh -c pip3 install --user etl --upgrade pip     && pip3 install --user etl -r /tmp/python_requirements.txt]: exit code: 1

我知道我可以通过在centos7中安装python-devel来解决MacOS中的这个问题,但我想了解为什么图像的行为不同。

谢谢。

python docker centos7 psutil
3个回答
4
投票

弗雷迪建议的答案对我有用。我将

--platform linux/amd64
添加到我的构建命令中。我计划将其添加到我的 m1 手臂上的所有未来 docker 命令中,尽管我承认我不知道哪些命令需要它或不需要它。

我运行的示例命令:

docker push --platform linux/amd64 brandojazz/iit-term-synthesis:test_arm
docker pull --platform linux/amd64 brandojazz/iit-term-synthesis:test_arm
docker build --platform linux/amd64 -f ~/iit-term-synthesis/Dockerfile_arm -t brandojazz/iit-term-synthesis:test_arm ~/iit-term-synthesis/
docker run -v /Users/brandomiranda/iit-term-synthesis:/home/bot/iit-term-synthesis \
           -v /Users/brandomiranda/pycoq:/home/bot/pycoq \
           -v /Users/brandomiranda/ultimate-utils:/home/bot/ultimate-utils \
           -v /Users/brandomiranda/proverbot9001:/home/bot/proverbot9001 \
           -v /Users/brandomiranda/data:/home/bot/data \
           --platform linux/amd64 \
           -ti brandojazz/iit-term-synthesis:test bash

详情请参阅:https://pythonspeed.com/articles/docker-build-problems-mac/


3
投票

经过长时间的搜索,我找到了这篇文章https://pythonspeed.com/articles/docker-build-problems-mac/

问题出在芯片架构(arm)上。上面文章中提出的解决方案正在发挥作用(要么从源代码构建,获取预编译的二进制文件,要么在使用 Docker 构建时添加

--platform linux/amd64


0
投票

为了可能使用

docker compose
的其他人的利益,现在您可以在服务容器上设置
platform: linux/amd64
来解决此类问题。

(我使用的是 Mac M1,遇到了这个问题,而不是修改

Dockerfile
修复了这个问题
docker-compose.yml

© www.soinside.com 2019 - 2024. All rights reserved.