如何在Alpine Linux容器上安装pyzmq?

时间:2018-08-19 06:38:46

标签: python-3.x docker pip alpine pyzmq

我有一个带有Nullable<T>内核的容器,我在此安装带有 pip pyzmq 时遇到问题:

Dockerfile:

c_id

docker-compose.yml:

c_id = (txtBox_id.Text != string.Empty) ? Convert.ToInt32(txtBox_id.Text) : (int?)null;;

在此容器上安装几个Python库时,我没有任何问题,但是在 pyzmq 库中,出现错误:

程序:

python:3.6-alpine

在容器中:

FROM python:3.6-alpine

RUN mkdir /code
RUN apk add vim
WORKDIR /
ADD . /code

上面也安装了命令,但是在 pyzmq 安装中,我遇到了以下错误:

version: '3'

services:
  battery_monitoring:
      build: .
      image: bm:1.0.0
      container_name: battery_monitoring
      restart: unless-stopped
      volumes:
        - .:/code
      tty: true

部分结果:

$ docker-compose build
$ docker-compose up -d
$ docker exec -it <This-container-ID> sh

[注意]:

pip install pyserial
pip install easydict

pip install pyzmq 而不是 ---------------------------------------- Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-545my4q5/pyzmq/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-nbtsgz0b/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-545my4q5/pyzmq/ 没什么问题

3 个答案:

答案 0 :(得分:6)

使用py3-zmq

根据我的经验,由于缺少Python标头,python:3.6-alpine不太适合安装具有C扩展名的软件包。 alpine图像已经提供了Python 3.6发行版,还提供了precompiled pyzmq package,因此已经足够了:

FROM alpine:edge

RUN apk update && apk add py3-zmq

CMD ["/bin/sh"]

检查:

$ docker run --rm -it my/alpine /bin/sh
/ # python3 -c "import zmq; print(zmq.__version__)"
17.1.0

这是将pyzmq安装在Alpine容器中的最简单,最可靠的方法。

使用pip install从源构建

Alpine不兼容许多Linux1,因此任何包含C扩展名的软件包都必须从源代码构建。这意味着您必须先安装构建工具。同样,我将使用alpine图片而不是python:3.6-alpine

FROM alpine:edge

RUN apk update && apk add build-base libzmq musl-dev python3 python3-dev zeromq-dev

RUN pip3 install pyzmq

# reduce image size by cleaning up the build packages
RUN apk del build-base musl-dev python3-dev zeromq-dev

CMD ["/bin/sh"]

检查:

$ docker run --rm -it my/alpine /bin/sh
/ # python3 -c "import zmq; print(zmq.__version__)"
17.1.0

如果您坚持使用python:3.6-alpine

请注意,python:3.6-alpine不会通过apk安装Python,它具有从源代码构建并位于/usr/local下的Python。因此,当您从python:3.6-alpine继承,安装python3-dev并运行pip install pyzmq时,最终将为Python 3.6.6构建pyzmq(来自python:3.6-alpine )使用来自Python 3.6.4(来自apk add python3-dev)的头文件。通常,这不应该成为问题(头文件仅在主要的Python版本之间是不兼容的),但是如果头文件由发行版维护人员进行了修改,可能会成为问题。

编辑:使用日志复制的确切步骤

$ docker image rm my/alpine:latest
Untagged: my/alpine:latest
Deleted: sha256:2e613cdc3c90c9d44b23d399bd44069217e5b31c1b4a8fc91e501c5226a4ef6a
Deleted: sha256:d66ac6c96a4fca9c4fe71a73b64a4dd3605a59e570f327974954649b633a7fc5
Deleted: sha256:114efba5527eb4ab23020ef84b6181b6a6ba790059b83ce046c9a1a6c0bdf419

$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine@sha256:79c2c5f6db53da44f90bb2731f29f725b5b14c378407a123776b6d3c76e6aebe
untagged: alpine@sha256:ae8a1f9146d74466ddf1def02088ba33544db9aceef01f4b388c674a5ad1d00b
deleted: sha256:5c4fa780951b060bb0a75355765bc58112350d9974970d60561671d552aaf2e2
deleted: sha256:c9e8b5c053a2dda62373bc57fa8cb634230a92ba5f02d2baf5d35b932d04a878

Total reclaimed space: 4.148MB

$ cat ./Dockerfile
FROM alpine:edge

RUN apk update && apk add py3-zmq

CMD ["/bin/sh"]

$ docker pull alpine:edge
edge: Pulling from library/alpine
a0710691c81a: Pull complete
Digest: sha256:8d9872bf7dc946db1b3cd2bf70752f59085ec3c5035ca1d820d30f1d1267d65d
Status: Downloaded newer image for alpine:edge

$ docker build -t my/alpine .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM alpine:edge
 ---> 9d1f27787d39
Step 2/3 : RUN apk update && apk add py3-zmq
 ---> Running in 0f9bd971b5da
fetch http://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
v3.8.0-1447-g6c9915aaa5 [http://dl-cdn.alpinelinux.org/alpine/edge/main]
v3.8.0-1459-g2ff55fde23 [http://dl-cdn.alpinelinux.org/alpine/edge/community]
OK: 9626 distinct packages available
(1/16) Installing libbz2 (1.0.6-r6)
(2/16) Installing expat (2.2.5-r0)
(3/16) Installing libffi (3.2.1-r4)
(4/16) Installing gdbm (1.13-r1)
(5/16) Installing xz-libs (5.2.4-r0)
(6/16) Installing ncurses-terminfo-base (6.1-r0)
(7/16) Installing ncurses-terminfo (6.1-r0)
(8/16) Installing ncurses-libs (6.1-r0)
(9/16) Installing readline (7.0.003-r0)
(10/16) Installing sqlite-libs (3.24.0-r1)
(11/16) Installing python3 (3.6.4-r1)
(12/16) Installing libgcc (6.4.0-r8)
(13/16) Installing libsodium (1.0.16-r0)
(14/16) Installing libstdc++ (6.4.0-r8)
(15/16) Installing libzmq (4.2.3-r0)
(16/16) Installing py3-zmq (17.1.0-r0)
Executing busybox-1.28.4-r0.trigger
OK: 69 MiB in 29 packages
Removing intermediate container 0f9bd971b5da
 ---> 83a4db72581d
Step 3/3 : CMD ["/bin/sh"]
 ---> Running in b37e3ef8e639
Removing intermediate container b37e3ef8e639
 ---> 558bd6427c77
Successfully built 558bd6427c77
Successfully tagged my/alpine:latest

$ docker run --rm -it my/alpine python3 -c "import zmq; print(zmq.__version__)"
17.1.0

答案 1 :(得分:0)

按照zeromq/pyzmq issue 1050

所述,检查它是否与python 3.7更好地兼容
  

.c文件是使用Cython生成的。似乎Python 3.7更改了C API,因此.c文件不再与Python 3.7兼容。我们需要等待Cython版本修复对未发布的Python的支持,然后才能使用它。

答案 2 :(得分:0)

感谢@hoefling,我首先在Alpine Linux v3.12上通过安装<https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv 534-5.7.14 bXAEJKkJeqWCiVzTTlBWq9rxftBEamfSnaWIVJ0_dKDfgIuJ6a7XbSNz62WFwSuLLEQAO 534-5.7.14 vcrBWQgPPNpki5WeHQlOd9wE2RNXirLYC1_og20m_xsWH4wka8pkDZJz0Iv-LySW> 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 c16sm931756lfb.236 - gsmtp 软件包解决了此依赖性问题。

  1. 运行Alpine 3.12的Docker容器。

    py3-pyzmq
  2. 安装[ chusiang@macOS-11.0.1 ~ ] $ docker run -it --rm alpine:3 sh 的apk软件包。

    py3-pyzmq
  3. 安装/home # apk add py3-pyzmq (1/3) Installing libsodium (1.0.18-r0) (2/3) Installing libzmq (4.3.3-r0) (3/3) Installing py3-pyzmq (18.1.1-r0) OK: 385 MiB in 97 packages 笔记本的pip3软件包。

    jupyter
  4. 完成!

    /home # pip3 install jupyter
    

顺便说一句,我引用这篇文章来升级我的docker image项目“ ansible-jupyter”上的Python 2-> 3。

https://github.com/chusiang/ansible-jupyter.dockerfile/commit/200c9a48232b0149adb85fa9e5fc025730691599