六、Docker部署深度学习模型,实现API端口通信(Ubuntu 16.04)

应用场景

  • docker部署深度学习模型
  • 使用 shell 激活conda环境
  • 使用 shell 将端口在后台挂载,实现api不同端口之间的相互通信

1. 拉取镜像

深度学习环境镜像jlgao/ubuntu16.04-cuda8-cudnn6-annconda2:v1.0: ubuntu16.04 + cuda8 + cudnn6 + ananconda2

junli@ubuntu16:~$ docker pull jlgao/ubuntu16.04-cuda8-cudnn6-annconda2:v1.0
junli@ubuntu16:~$ docker images
REPOSITORY                                 TAG                            IMAGE ID       CREATED         SIZE
jlgao/ubuntu16.04-cuda8-cudnn6-annconda2   v1.0                           959c95a91678   26 hours ago    4.01GB

2. 创建容器

junli@ubuntu16:~$ docker run --runtime=nvidia -i -d --net GJLDockerNetBridge --name flask-test -v /home/junli/GJLImages/flask/:/home/workdir jlgao/ubuntu16.04-cuda8-cudnn6-annconda2:v1.0 /bin/bash
b77f8e760cc531e18bb0aecf73062a34523c0613af75e6a17094c7da7691693d
junli@ubuntu16:~$ docker ps -a
CONTAINER ID   IMAGE                                           COMMAND       CREATED         STATUS                        PORTS     NAMES
b77f8e760cc5   jlgao/ubuntu16.04-cuda8-cudnn6-annconda2:v1.0   "/bin/bash"   8 seconds ago   Up 7 seconds                            flask-test

3. 将本地文件复制到容器里

junli@ubuntu16:~$ docker cp tools/anaconda3/envs/flask-test flask-test:/root/anaconda2/envs/
junli@ubuntu16:~$ docker cp codes/vscode_projects/flask_test flask-test:/home/workdir/
## 注意:创建容器后,需要将conda环境(这里是/root/anaconda2)添加到/root/.bashrc中,可以手动添加,也可以写到shell脚本中。
## 此篇是将conda环境激活添加到shell脚本中。

4. 查看容器占用的端口号

junli@ubuntu16:~$ docker inspect flask
....
{  
    ......
    "NetworkSettings": {
        ......
        "Networks": {
            "GJLDockerNetBridge": {
                ......
                "Gateway": "192.168.120.1",
                "IPAddress": "192.168.120.2",
                "IPPrefixLen": 24,
                ......
            }
        }
    }
}

5. 运行并查看结果

junli@ubuntu16:~$ docker exec -it flask-test /bin/bash
root@b77f8e760cc5:/# cd home/workdir/flask_test/
root@b77f8e760cc5:/home/workdir/flask_test# ls
api.py  logs  start_ports.sh
root@b77f8e760cc5:/home/workdir/flask_test# bash start_ports.sh

可以在postman查看看结果
六、Docker部署深度学习模型,实现API端口通信(Ubuntu 16.04)_第1张图片

  • api.py文件内容
from flask import Flask, request, jsonify

# initialize our Flask application and the Keras model
app = Flask(__name__)

@app.route("/", methods=["POST"])
def handleImage():
    if request.method == "POST":
        try: 
            results = {}
            name = request.form["name"]
            
            results['id']   = '123456'
            results['name'] = name
            
            return jsonify(results)
        except Exception as e:
            return {'error_msg': str(e)}

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=3456, debug=False)

  • start_ports.sh文件内容
    • step1:激活制定conda环境;
    • step2:后台执行 /home/workdir/flask_test 目录下的 api.py 文件,实现端口持续监听
#!/bin/sh
## step1: 激活conda环境
__conda_setup="$('/root/anaconda2/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/anaconda2/etc/profile.d/conda.sh" ]; then
        . "/root/anaconda2/etc/profile.d/conda.sh"
    else
        export PATH="/root/anaconda2/bin:$PATH"
    fi
fi
unset __conda_setup

source activate flask-test

## step2: 后台运行python程序
nohup python -u api.py >logs/log_api.log 2>&1 &

source deactivate
总结常用语法
docker pull IMAGE:TAG
docker run --runtime=nvidia -i -d --net NetBridge --name CONTAINER_NAME -v local_path/workdir:docker_path/workdir IMAGE:TAG /bin/bash
docker cp local_path/workdir/project CONTAINER_NAME:docker_path/workdir/project
docker cp local_path/anacondaX/envs/ENV_NAME CONTAINER_NAME:docker_path/anacondaX/envs/
docker exec -it CONTAINER_NAME /bin/bash

你可能感兴趣的:(Docker,docker,深度学习,ubuntu)