1、安装Docker
参考文档:
2、Python机器学习工具的Docker镜像
镜像基于 Alpine Linux Python 3.5 镜像,只有 60MB 镜像,并且包含流行的机器学习(Machine Leaning) 工具(numpy、pandas、scipy、scikit-learn)。获取Python机器学习工具的docker镜像,有两种方式,一种是通过docker pull命令获取,另一种是通过Dockerfile文件创建,具体如下,
1)使用docker pull命令
使用docker pull命令获取https://hub.docker.com/
中存储公共镜像,如下,
docker pull frolvlad/alpine-python-machinelearning
注意:使用docker run命令时,如果镜像不存会自动调用docker pull获取镜像。
2)使用Dockerfile文件创建
Dockerfile文件:
FROM frolvlad/alpine-python3 RUN apk add --no-cache \ --virtual=.build-dependencies \ g++ gfortran file binutils \ musl-dev python3-dev cython openblas-dev lapack-dev && \ apk add libstdc++ openblas lapack && \ \ ln -s locale.h /usr/include/xlocale.h && \ \ pip install --disable-pip-version-check --no-build-isolation numpy && \ pip install --disable-pip-version-check --no-build-isolation pandas && \ \ # scipy 1.4.x releases are broken on Alpine due to: https://github.com/scipy/scipy/issues/11319 #pip install --disable-pip-version-check --no-build-isolation scipy && \ apk add --no-cache --virtual=.build-dependencies-scipy-patch patch && \ cd /tmp && \ SCIPY_VERSION=1.4.1 && \ wget "https://github.com/scipy/scipy/releases/download/v$SCIPY_VERSION/scipy-$SCIPY_VERSION.tar.xz" && \ tar -xJf "scipy-$SCIPY_VERSION.tar.xz" && \ (cd "scipy-$SCIPY_VERSION" && wget https://patch-diff.githubusercontent.com/raw/scipy/scipy/pull/11320.patch -O - | patch -p1) && \ pip install --disable-pip-version-check --no-build-isolation "/tmp/scipy-$SCIPY_VERSION/" && \ rm -rf /tmp/* && \ apk del .build-dependencies-scipy-patch && \ \ pip install --disable-pip-version-check --no-build-isolation scikit-learn && \ \ rm -r /root/.cache && \ find /usr/lib/python3.*/ -name 'tests' -exec rm -r '{}' + && \ find /usr/lib/python3.*/site-packages/ -name '*.so' -print -exec sh -c 'file "{}" | grep -q "not stripped" && strip -s "{}"' \; && \ \ rm /usr/include/xlocale.h && \ \ apk del .build-dependencies # Add pycddlib and cvxopt with GLPK RUN cd /tmp && \ apk add --no-cache \ --virtual=.build-dependencies \ gcc make file binutils \ musl-dev python3-dev cython gmp-dev suitesparse-dev openblas-dev && \ apk add gmp suitesparse && \ \ pip install --disable-pip-version-check --no-build-isolation pycddlib && \ \ wget "ftp://ftp.gnu.org/gnu/glpk/glpk-4.65.tar.gz" && \ tar xzf "glpk-4.65.tar.gz" && \ cd "glpk-4.65" && \ ./configure --disable-static && \ make -j4 && \ make install-strip && \ CVXOPT_BLAS_LIB=openblas CVXOPT_LAPACK_LIB=openblas CVXOPT_BUILD_GLPK=1 pip install --disable-pip-version-check --no-build-isolation --global-option=build_ext --global-option="-I/usr/include/suitesparse" cvxopt && \ \ rm -r /root/.cache && \ find /usr/lib/python3.*/site-packages/ -name '*.so' -print -exec sh -c 'file "{}" | grep -q "not stripped" && strip -s "{}"' \; && \ \ apk del .build-dependencies && \ rm -rf /tmp/*
生成本地镜像:
docker build -t cjavapy/alpine-python-machinelearning .
3、Python机器学习工具的Docker容器
创建容器可以使用https://hub.docker.com/
上的frolvlad/alpine-python-machinelearning
镜像,也可以使用上面我们通过Dockerfile文件创建的本地镜像。
1)使用frolvlad/alpine-python-machinelearning镜像
$ docker run --rm frolvlad/alpine-python-machinelearning python3 -c 'import numpy; print(numpy.arange(3))'
2)使用cjavapy/alpine-python-machinelearning镜像
$ docker run --rm cjavapy/alpine-python-machinelearning python3 -c 'import numpy; print(numpy.arange(3))'
相关文档: