简体   繁体   English

如何从 Dockerfile 运行环境初始化 shell 脚本

[英]How to run environment initialization shell script from Dockerfile

I am trying to build an API wrapped in a docker image that serves Openvino model.我正在尝试构建一个包含在为 Openvino 模型提供服务的 docker 映像中的 API。 How do I run the "setupvars.sh" from Dockerfile itself so that my application can access it?如何从 Dockerfile 本身运行“setupvars.sh”,以便我的应用程序可以访问它?

I have tried running the script using RUN.我尝试使用 RUN 运行脚本。 For ex: RUN /bin/bash setupvars.sh or RUN ./setupvars.sh .例如: RUN /bin/bash setupvars.sh 或 RUN ./setupvars.sh 。 However, none of them work and I get ModelNotFoundError: no module named openvino但是,它们都不起作用,我得到 ModelNotFoundError: no module named openvino

RUN $INSTALL_DIR/install_dependencies/install_openvino_dependencies.sh
RUN cd /opt/intel/openvino/deployment_tools/model_optimizer/install_prerequisites && sudo ./install_prerequisites_tf.sh
COPY . /app
WORKDIR /app
RUN apt autoremove -y && \
    rm -rf /openvino /var/lib/apt/lists/*
RUN /bin/bash -c "source $INSTALL_DIR/bin/setupvars.sh"
RUN echo "source $INSTALL_DIR/bin/setupvars.sh" >> /root/.bashrc
CMD ["/bin/bash"]
RUN python3 -m pip install opencv-python
RUN python3 test.py

I want OpenVino accessible to my gunicorn application that will serve the model in a docker image我希望我的 gunicorn 应用程序可以访问 OpenVino,该应用程序将在 docker 映像中为模型提供服务

Next commands works for me.下一个命令对我有用。

ARG OPENVINO_DIR=/opt/intel/computer_vision_sdk


# Unzip the OpenVINO installer
RUN cd ${APP_DIR} && tar -xvzf l_openvino_toolkit*

# installing OpenVINO dependencies
RUN cd ${APP_DIR}/l_openvino_toolkit* && \
    ./install_cv_sdk_dependencies.sh

# installing OpenVINO itself
RUN cd ${APP_DIR}/l_openvino_toolkit* && \
    sed -i 's/decline/accept/g' silent.cfg && \
    ./install.sh --silent silent.cfg

# Setup the OpenVINO environment
RUN /bin/bash -c "source ${OPENVINO_DIR}/bin/setupvars.sh"

You need to re-run it every time you start the container, because those variables are only for the session.每次启动容器时都需要重新运行它,因为这些变量仅用于会话。

Option 1: Run your application something like this:选项 1:像这样运行您的应用程序:

CMD /bin/bash -c "source /opt/intel/openvino/bin/setupvars.sh && python test.py"

Option 2 (not tested): Add the source command to your .bashrc so it will be run every time on startup选项 2(未测试):将 source 命令添加到您的 .bashrc 以便每次启动时都会运行

# Assuming running as root
RUN echo "/bin/bash -c 'source /opt/intel/openvino/bin/setupvars.sh'" >> ~root/.bashrc
CMD python test.py

For the rest of the Dockerfile, there is a guide here (also not tested, and it doesn't cover the above): https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_docker_linux.html对于 Dockerfile 的其余部分,这里有一个指南(也没有经过测试,不包括上述内容): https : //docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_docker_linux.html

As mentioned in the two previous answers, the setupvars.sh script sets the environment variables required by OpenVINO.如前两个答案所述, setupvars.sh脚本设置 OpenVINO 所需的环境变量。

But rather than running this every time, you can add the variables to your Dockerfile.但是,您可以将变量添加到 Dockerfile 中,而不是每次都运行它。 While writing your Dockerfile run:在编写 Dockerfile 时运行:

CMD /bin/bash -c "source /opt/intel/openvino/bin/setupvars.sh && printenv"

This will give you the values that the environment variables are set to.这将为您提供环境变量设置的值。 You might also want to run printenv without setting the OpenVINO variables:您可能还想在不设置 OpenVINO 变量的情况下运行printenv

CMD /bin/bash printenv

Comparing the two outputs will let you figure out exactly what the setupvars.sh script is setting.比较这两个输出可以让您准确地找出setupvars.sh脚本正在设置的内容。

Once you know the values set by the script, you can set these as part of the Dockerfile using the ENV instruction.一旦知道脚本设置的值,就可以使用ENV指令将这些值设置为 Dockerfile 的一部分。 I wouldn't copy this because it's likely to be specific to your setup, but in my case, this ended up looking like:不会复制它,因为它可能特定于您的设置,但就我而言,这最终看起来像:

ENV PATH=/opt/intel/openvino/deployment_tools/model_optimizer:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV LD_LIBRARY_PATH=/opt/intel/openvino/opencv/lib:/opt/intel/openvino/deployment_tools/ngraph/lib:/opt/intel/openvino/deployment_tools/inference_engine/external/tbb/lib::/opt/intel/openvino/deployment_tools/inference_engine/external/hddl/lib:/opt/intel/openvino/deployment_tools/inference_engine/external/omp/lib:/opt/intel/openvino/deployment_tools/inference_engine/external/gna/lib:/opt/intel/openvino/deployment_tools/inference_engine/external/mkltiny_lnx/lib:/opt/intel/openvino/deployment_tools/inference_engine/lib/intel64
ENV INTEL_CVSDK_DIR=/opt/intel/openvino
ENV OpenCV_DIR=/opt/intel/openvino/opencv/cmake
ENV TBB_DIR=/opt/intel/openvino/deployment_tools/inference_engine/external/tbb/cmake
# The next one will be whatever your working directory is
ENV PWD=/workspace
ENV InferenceEngine_DIR=/opt/intel/openvino/deployment_tools/inference_engine/share
ENV ngraph_DIR=/opt/intel/openvino/deployment_tools/ngraph/cmake
ENV SHLVL=1
ENV PYTHONPATH=/opt/intel/openvino/python/python3.6:/opt/intel/openvino/python/python3:/opt/intel/openvino/deployment_tools/tools/post_training_optimization_toolkit:/opt/intel/openvino/deployment_tools/open_model_zoo/tools/accuracy_checker:/opt/intel/openvino/deployment_tools/model_optimizer
ENV HDDL_INSTALL_DIR=/opt/intel/openvino/deployment_tools/inference_engine/external/hddl
ENV _=/usr/bin/printenv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM