# 5.2 PCIE加速卡模式

#### 5.2.1 下载基础镜像：x86\_sophonsdk3\_ubuntu18.04\_py37\_runtime\_22.06.docker

#### 5.2.2 构建自定义镜像

Dockerfile示例（仅供参考）：Dockerfile.x86

```docker
# 指定基础镜像
FROM sophgo/sophonsdk3:ubuntu18.04-py37-runtime-22.06

# 维护者信息
MAINTAINER docker_user docker_user@email.com

# 安装基本软件包
RUN apt-get update --fix-missing \
 && apt-get install -y gcc vim libglib2.0-dev \
 && apt-get -y install locales

# 设置时区
ENV TIME_ZONE Asia/Shanghai
RUN apt-get install -y tzdata \
    && ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata

# 安装nginx/redis/sqlite3/supervisor
RUN apt-get -y install nginx \
 && apt-get -y install redis-server redis-tools \
 && apt-get -y install sqlite3 \
 && apt-get -y install supervisor
 
# 安装mysql
RUN { \
    echo mysql-server-5.7 mysql-server/root_password password '123456'; \
    echo mysql-server-5.7 mysql-server/root_password_again password '123456'; \
} | debconf-set-selections \
 && apt-get -y install mysql-server mysql-client

# 安装调试工具软件
RUN apt-get -y install htop dstat sysstat iptraf-ng traceroute curl

# 更换pip源
RUN mkdir -p /root/.pip \
 && touch /root/.pip/pip.conf \
 && echo "[global]" >> /root/.pip/pip.conf \
 && echo "index-url = http://mirrors.aliyun.com/pypi/simple/" >> /root/.pip/pip.conf \
 && echo "[install]" >> /root/.pip/pip.conf \
 && echo "trusted-host = mirrors.aliyun.com" >> /root/.pip/pip.conf
 
# 更新pip，安装numpy
RUN python3 -m pip install --upgrade pip \
 && python3 -m pip install numpy

# 安装python依赖软件包
RUN python3 -m pip install pillow \
 && python3 -m pip install xlwt

# 创建文件夹
RUN mkdir -p /workspace/conf \
 && mkdir -p /workspace/log/supervisor \
 && mkdir -p /workspace/log/redis \
 && mkdir -p /workspace/log/mysql

# 设置环境变量
ENV LANG "C.UTF-8"
ENV PATH "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/system/bin:/usr/sbin:/bm_bin:${PATH}"
ENV LD_LIBRARY_PATH "/system/lib:${LD_LIBRARY_PATH}"
ENV PYTHONPATH "/system/lib:${PYTHONPATH}"
ENV GSETTINGS_SCHEMA_DIR "/workspace/conf/gsettings"

# 清理无用软件包
RUN apt-get autoremove -y --purge gcc \
 && apt-get clean \
 && rm -rf /var/cache/apt/archives/*

RUN apt-get clean \
 && ldconfig \
 && rm -rf /var/lib/apt/lists/*
```

使用Dockerfile构建镜像的脚本（仅供参考）：build\_demo\_image\_on\_x86.sh

```bash
#!/bin/bash

currTime=$(date +"%Y%m%d")
TARGET_ARCH="x86"
REP_NAME="demo"
VERSION="v1.0.0"
BASE_IMAGE="sophonsdk3-runtime-ubuntu1804"

REPOSITORY="${TARGET_ARCH}/${REP_NAME}"
TAG="${VERSION}-${BASE_IMAGE}"
DockerBuildFile="Dockerfile.x86"
DockerImageFile="images/${TARGET_ARCH}_${BASE_IMAGE}_${REP_NAME}_${VERSION}_${currTime}.tar.gz"

echo "========================================================================="

echo $(date +"%Y-%m-%d %T")
echo "========================================================================="
echo " build info: "
echo "========================================================================="
echo "Dockerfile using:       ${DockerBuildFile}"
echo "Target image:           ${REPOSITORY}:${TAG}"
echo "Target file:            ${DockerImageFile}"
echo "========================================================================="
echo "start to build docker"
echo "========================================================================="
docker build -t ${REPOSITORY}:${TAG} -f ${DockerBuildFile} .
echo "========================================================================="
docker images | grep "${REPOSITORY}" | grep "${TAG}"
if [ $? -ne 0 ] ;then
    echo "build ${REPOSITORY}:${TAG} failed"
    exit -1
fi

echo "========================================================================="
echo "build ${REPOSITORY}:${TAG} success"
echo "========================================================================="
echo "start to save file ..."
docker save ${REPOSITORY}:${TAG} | gzip > ${DockerImageFile}
echo "========================================================================="
if [ $? -ne 0 ] ;then
    echo "save to file failed"
else
    echo "save to file ${DockerImageFile} success"
fi
echo "========================================================================="
```

#### 5.2.3 加载docker镜像

```bash
docker load -i xxxxxxxxx.tar.gz
```

#### 5.2.4 创建docker容器运行

运行脚本文件（仅供参考）：run\_demo\_docker\_on\_x86.sh

```bash
#!/bin/bash

###  x86/demo   v1.0.0-sophonsdk3-runtime-ubuntu1804

REPO="x86"
IMAGE="demo"
TAG="v1.0.0-sophonsdk3-runtime-ubuntu1804"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo $DIR
echo "$REPO/$IMAGE:$TAG"

if [ -c "/dev/bm-sophon0" ]; then
  for dev in $(ls /dev/bm-sophon*);
  do
    mount_options+="--device="$dev:$dev" "
  done
  CMD="docker run \
      --privileged=true \
      --network=bridge \
      --workdir=/workspace \
      ${mount_options} \
      --device=/dev/bmdev-ctl:/dev/bmdev-ctl \
      -p 80:80 \
      -v /dev/shm --tmpfs /dev/shm:exec \
      -v /dev:/dev \
      -v /etc/localtime:/etc/localtime \
      -e LOCAL_USER_ID=`id -u` \
      -itd $REPO/$IMAGE:$TAG \
      /workspace/start.sh
  "
else
  echo "No Sophon Series Deep Learning Accelerator, docker will run in CMode"
  CMD="docker run \
      --privileged=true \
      --network=bridge \
      --workdir=/workspace \
      -p 80:80 \
      -v /dev/shm --tmpfs /dev/shm:exec \
      -v /etc/localtime:/etc/localtime \
      -e LOCAL_USER_ID=`id -u` \
      -itd $REPO/$IMAGE:$TAG \
      bash
  "
fi

echo $CMD
eval $CMD
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sophgo-doc.gitbook.io/sophonsdk3/deploy/pcie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
