参考:
首先查看构建器builder:
docker buildx ls
如果在支持的架构里面看到没有arm架构的版本,则先运行:
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
然后创建自己的builder,并查看信息:
docker buildx rm builder
docker buildx create --name builder --driver docker-container --use
docker buildx inspect --bootstrap
如:
用于树莓派的docker镜像中,需要安装基于arm架构的TensorFlow。树莓派提供了一个arm架构的python wheel包的仓库:
但是如果直接使用pip从该仓库安装,可能会遇到部分依赖包找不到的问题。此时,需要先从python官方仓库,安装找不到的依赖包,最后再安装TensorFlow。
下面展示一个安装TensorFlow 1.13.1 版本镜像的Dockerfile
FROM ubuntu:18.04
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN apt-get update && apt-get install -y
ca-certificates curl wget software-properties-common git libgl1-mesa-glx \
libhdf5-dev libhdf5-serial-dev libatlas-base-dev libqtgui4 libqt4-test \
libsm6 libxrender1 libxext-dev cmake pkg-config
RUN apt-get install -y python3.7 python3.7-dev python3-pip build-essential python3-dev python3-setuptools
RUN rm /usr/bin/python3
RUN ln -s /usr/bin/python3.7 /usr/bin/python && ln -s /usr/bin/python3.7 /usr/bin/python3
RUN alias python=python3 && ln -s /usr/bin/pip3 /usr/bin/pip
ENV PYTHONIOENCODING=utf-8
WORKDIR /app
RUN python3 -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install h5py scikit-build tensorflow-estimator==1.14.0 tensorboard==1.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install tensorflow==1.13.1 -i https://www.piwheels.org/simple
在安装tensorflow==1.13.1之前,安装了一些依赖库。其中安装h5py,需要python3.7的环境(以及libhdf5-dev等各种依赖库),否则可能安装失败,所以前面将基础镜像自带的python版本升级为3.7。
Dockerfile编写完成后,使用docker buildx命令构建所需架构的镜像,如:
docker buildx build --platform linux/arm/v7 -t username/imagename --push .
其中,--platform
后面接架构名字,-t
后面接仓库及镜像名,--push
表示推送到仓库。(须推送到仓库或者导出成文件,build完成后,使用docker images是看不到打出的镜像的)
推送完成后,即可在树莓派中pull下来,然后使用