13 篇文章带有标签 “dockerfile”

Dockerfile 中 ARG 指令的作用范围

这里主要是了解 Dockerfile 中 ARG 指令的作用范围。

总结

  1. FROM 前定义的参数,只能在 FROM 指令中使用,且能够在多阶段中起作用。
ARG BASE_IMAGE=python:3.10.9
FROM ${BASE_IMAGE} AS builder
...

FROM ${BASE_IMAGE}
...
  1. FROM 后定义的参数,只能作用在本 Stage 阶段。
FROM python:3.10.9 AS builder
ARG APP_HOME=/WALL-E-AI
WORKDIR ${APP_HOME}

FROM python:3.10.9
ARG APP_HOME=/WALL-E-AI
WORKDIR ${APP_HOME}

实验

构建容器化 Python 应用程序

这里使用 Ultralytics Serving 作为示例,它是一个基于 FastAPIUltralytics YOLOv8 的模型推理服务。

选择 Python 镜像

Tag Python Version OS Version Size
3.10 3.10 Debian GNU/Linux 11 (bullseye) 861MB
3.10-slim 3.10 Debian GNU/Linux 11 (bullseye) 114MB
3.10-alpine 3.10 Alpine Linux 3.15.0 44.7MB

克隆 Ultralytics Serving

git clone https://github.com/gouchicao/ultralytics-serving.git
cd ultralytics-serving

编写 Dockerfile

采用两阶段构建,第一阶段安装依赖环境和编译应用,第二阶段发布应用。第二阶段可以使用小一点的镜像,比如 python:3.10-slimpython:3.10-alpine

普通版本 FROM python:3.10 AS builder ENV APP_HOME=/ultralytics-serving WORKDIR ${APP_HOME} # 提前安装,因为 cpu 版本需要指定 index-url。

加速 Docker 构建镜像

查看镜像信息

操作系统版本

cat /etc/os-release 
  • Debian
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
  • Ubuntu
PRETTY_NAME="Ubuntu Jammy Jellyfish (development branch)"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04 (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

Dockerfile 实践

系统

指定本地时区

FROM ubuntu:20.04
LABEL maintainer="wang-junjian@qq.com"

ARG TIME_ZONE=Asia/Shanghai
RUN DEBIAN_FRONTEND=noninteractive apt-get install tzdata -y && \
    ln -fs /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && \
    echo $TIME_ZONE > /etc/timezone

Python 开发环境 构建 Python, OpenCV 开发环境的镜像 FROM ubuntu:20.04 LABEL maintainer="wang-junjian@qq.com" # 设置 apt 源(阿里云),设置完必须 update。 RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.

构建基于PaddlePaddle开发服务镜像

构建镜像

FROM paddlepaddle/paddle:2.2.2-gpu-cuda10.2-cudnn7
LABEL maintainer="wang-junjian@qq.com"

RUN apt-get update && apt-get install libjpeg-dev zlib1g-dev -y

RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ \
    numpy fastapi paddleocr opencv-python

EXPOSE 20000

WORKDIR /inference-serving
ADD . ./

CMD ["python", "app.py"]

官方推荐:非安培架构的GPU,推荐使用CUDA10.2,性能更优。

自己构建 paddlepaddle 镜像

通过官方的 Docker Hub 没有找到 runtime 版本,想着节省几个G的空间,于是考虑自己来构建。

构建基于 ONNXRuntime 的推理服务

构建 ONNXRuntime-GPU 镜像

编写 requirements.txt

$ vim requirements.txt
flask
connexion[swagger-ui]
connexion
gunicorn
numpy
opencv-python
scikit-image
psutil
pynvml
onnxruntime-gpu

编写 Dockerfile 需要带 cudnn 库的 CUDA 作为基镜像 $ vim Dockerfile FROM nvidia/cuda:11.4.0-cudnn8-runtime-ubuntu20.04 LABEL maintainer="wang-junjian@qq.com" RUN rm /etc/apt/sources.list.d/cuda.list /etc/apt/sources.list.d/nvidia-ml.list && \ sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.

GaiaGPU: 在容器云中共享GPU

容器技术由于其轻量级和可伸缩的优势而被广泛使用。GPU也因为其强大的并行计算能力被用于应用程序加速。在云计算环境下,容器可能需要一块或者多块GPU计算卡来满足应程序的资源需求,但另一方面,容器独占GPU计算卡常常会带来资源利用率低的问题。因此,对于云计算资源提供商而言,如何解决在多个容器之间共享GPU计算卡是一个很有吸引力的问题。本文中我们提出了一种称为GaiaGPU的方法,用于在容器间共享GPU存储和GPU的计算资源。GaiaGPU会将物理GPU计算卡分割为多个虚拟GPU并且将虚拟GPU按需分配给容器。同时我们采用了弹性资源分配和动态资源分配的方法来提高资源利用率。实验结果表明GaiaGPU平均仅带来1.015%的性能损耗并且能够高效的为容器分配和隔离GPU资源。

编译 GaiaGPU 服务

配置 git 加速

$ git config --global url."https://github.com.cnpmjs.org".insteadOf "https://github.com"

$ vim /etc/profile
export GOPROXY=https://goproxy.cn,direct
export GO111MODULE=on
$ source /etc/profile

vCUDA Controller $ git clon

ConfigMap和Secret:配置应用程序

ConfigMap

向容器传递命令行参数

在 Docker 中定义命令与参数

下面是 Dockerfile 中的指令 ENTRYPOINT 和 CMD。

指令 解释
ENTRYPOINT 容器启动时调用的命令
CMD 传递给 ENTRYPOINT 指定命令的参数

构建带参数的程序(date:args)

编写脚本 date.sh

#!/bin/sh
INTERVAL=$1
while :
do
  echo $(date)
  sleep $INTERVAL
done

编写 Dockerfile

FROM busybox
ADD date.sh /date.sh
RUN chmod +x /date.sh
ENTRYPOINT ["/date.sh"]
CMD ["1"]

参数默认值为 1,在运行容器可以设置参数覆盖默认值。

构建镜像

docker build -t wangjunjian/date:args .

在 Docker 中覆盖命令和参数

使用 Docker 运行镜像来设置命令和参数

docker run [--entrypoint=] <image> [arg1, arg2, arg3]

启动镜像 $ docker run wangjunjian/date:args Tue Aug 3 13:08:04 UTC 2021 Tue Aug 3

在Kubernetes上运行第一个应用

基于Node构建应用

app.js

const http = require('http');
const os = require('os');

console.log("Kubia server starting...");

var handler = function(request, response) {
  console.log("Received request from " + request.connection.remoteAddress);
  response.writeHead(200);
  response.end("You've hit " + os.hostname() + "\n");
};

var www = http.createServer(handler);
www.listen(8080);

Dockerfile

FROM node:16-slim
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]

构建

docker build -t wangjunjian/kubia .

运行 docker run -d -p 8080:8080 wangjun

AI 模型打包发布

工程目录

model-package-release/
├── Dockerfile
├── Dockerfile.ubuntu
├── main.sh
└── .ssh
    ├── id_rsa
    ├── id_rsa.pub
    └── known_hosts

main.sh #!/usr/bin/sh # 模型和配置文件 FILE_CONFIG=config.yaml FILE_MODEL=model.onnx # 用于构建模型压缩包的目录结构 DIR_MODELS=models if [ -z $1 ] then echo "Usage: " echo "Environment variable:" echo " MODEL_SERVER_USERNAME Default: username" echo " MODEL_SERVER_IP Default: ip" echo "" echo "docker run --rm -v /home/ai/models/sign.yaml:/app/config.yaml \" echo " -v /home/ai/models/sign.onnx:/app/model.

Dockerfile OpenCV4 Ubuntu20.04

Dockerfile

FROM ubuntu:20.04
LABEL maintainer="wang-junjian@qq.com"

#auto install tzdata(opencv depend)
ENV DEBIAN_FRONTEND=noninteractive

RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN apt-get update && apt-get install -y \
    python3 python3-pip \
    libglib2.0-dev libgl1-mesa-glx \
    && rm -rf /var/lib/apt/lists/*

RUN ln -s /usr/bin/python3 /usr/bin/python && \
    ln -s /usr/bin/pip3 /usr/bin/pip
RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ \
    opencv-python opencv-contrib-python

#set your localtime
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR /

CMD bash

Dockerfile ONNXRuntime GPU

FROM nvidia/cuda:11.1-cudnn8-devel-ubuntu20.04 AS builder
LABEL maintainer="wang-junjian@qq.com"

#E: Failed to fetch https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu2004/x86_64/by-hash/SHA256/f10fc2a7a0d072ddcf141af2ef28f1e97ab4b3a5c3b9bbe34ed845d174fb4979  404  Not Found [IP: 61.155.167.2 443]
#E: Some index files failed to download. They have been ignored, or old ones used instead.
RUN rm /etc/apt/sources.list.d/cuda.list /etc/apt/sources.list.d/nvidia-ml.list

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install language-pack-en git python3 python3-pip -y && \
    DEBIAN_FRONTEND=noninteractive apt-get install cmake -y && \
    locale-gen en_US.UTF-8 && \
    update-locale LANG=en_US.UTF-8

RUN pip3 install numpy -i https://mirrors.aliyun.com/pypi/simple/
// ...

Dockerfile JDK-MCR(MATLAB Compiler Runtime)

JDK-MCR(MATLAB Compiler Runtime)

FROM openjdk:latest
LABEL maintainer="wang-junjian@qq.com"

MAINTAINER Wang Junjian

RUN apt-get update && apt-get install -y curl wget unzip xorg

#Install MATLAB runtime
RUN mkdir /mcr-install && cd /mcr-install && \
    wget -nv http://ssd.mathworks.com/supportfiles/downloads/R2018b/deployment_files/R2018b/installers/glnxa64/MCR_R2018b_glnxa64_installer.zip && \
    unzip MCR_R2018b_glnxa64_installer.zip && \
    ./install -mode silent -agreeToLicense yes && \
    rm -Rf /mcr-install

ENV LD_LIBRARY_PATH /usr/local/MATLAB/MATLAB_Runtime/v95/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/extern/bin/glnxa64

CMD /bin/sh