如何在docker中运行一个容器_好用的docker容器

(109) 2024-06-03 13:01:01

如何在Docker中运行PaddleServing

环境

Python 2.7.5

该文档以Python2为例展示如何在Docker中运行Paddle Serving,您也可以通过将python更换成python3来用Python3运行相关命令。

CPU版本

获取镜像

docker pull hub.baidubce.com/paddlepaddle/serving:latest

创建容器并进入

docker run -p 9292:9292 --name test -dit hub.baidubce.com/paddlepaddle/serving:latest
docker exec -it test bash

-p选项是为了将容器的9292端口映射到宿主机的9292端口。

安装PaddleServing

为了减小镜像的体积,镜像中没有安装Serving包,要执行下面命令进行安装。

/usr/bin/python -m pip install --upgrade pip
pip install paddle-serving-server

您可能需要使用国内镜像源(例如清华源)来加速下载。

pip install paddle-serving-server -i https://pypi.tuna.tsinghua.edu.cn/simple
如果安装serving遇到问题(sentencepiece的问题)

手动安装sentencepiece
pip install sentencepiece==0.1.82 (直接pip install paddle-serving-server遇到问题,手动执行命令安装sentencepiece)

安装Paddle Serving Client

在rpc方式调用的时候,需要引入“ from paddle_serving_client import Client”
在此情况下安装paddle-serving-client。
pip install paddle-serving-client==0.3.2

测试example

通过下面命令获取训练好的Boston房价预估模型:

wget --no-check-certificate https://paddle-serving.bj.bcebos.com/uci_housing.tar.gz
tar -xzf uci_housing.tar.gz
  • 测试HTTP服务

    在Server端(容器内)运行:

    python -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9292 --name uci >std.log 2>err.log &
    

    在Client端(容器内或容器外)运行:

    curl -H "Content-Type:application/json" -X POST -d '{"feed":{"x": [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727, -0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]}, "fetch":["price"]}' http://127.0.0.1:9292/uci/prediction
    
  • 测试RPC服务

    在Server端(容器内)运行:

    python -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9292 >std.log 2>err.log &
    

    在Client端(容器内或容器外,需要安装paddle-serving-client包)运行下面Python代码:

    from paddle_serving_client import Client
    
    client = Client()
    client.load_client_config("uci_housing_client/serving_client_conf.prototxt")
    client.connect(["127.0.0.1:9292"])
    data = [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727,
            -0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]
    fetch_map = client.predict(feed={ 
         "x": data}, fetch=["price"])
    print(fetch_map)
    

教程地址:

https://github.com/PaddlePaddle/Serving/blob/develop/doc/RUN_IN_DOCKER_CN.md

THE END

发表回复