BruceFan's Blog

Stay hungry, stay foolish

0%

Docker安装使用MySQL和Tensorflow Serving

Ubuntu18.04安装

安装最新版Docker

1
$ wget -qO- https://get.docker.com/ | sh

Docker可以在容器内运行应用程序,使用docker run命令在容器内运行应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ sudo docker

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/home/fanrong1/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/fanrong1/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/fanrong1/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/fanrong1/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
engine Manage the docker engine
...

使用Docker MySQL

下面以docker mysql为例介绍Docker的使用。
查找Docker Hub上的MySQL镜像

1
2
3
4
5
6
7
8
9
$ sudo docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8115 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2753 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 607 [OK]
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 191 [OK]
hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 113
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 100 [OK]
...

拉取官方镜像,Tag为5.7

1
$ sudo docker pull mysql:5.7

查看本地镜像列表

1
2
3
4
5
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 9361ce633ff1 8 weeks ago 118MB
tensorflow/serving latest 38bee21b2ca0 2 months ago 229MB
mysql 5.7 e47e309f72c8 3 months ago 372MB

启动mysql docker容器

1
2
$ sudo docker run --name test_docker_mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
65b2b644555b3730ac936caab2ccf9ad451b6e31f2509ed3e2333c9e20079873

查看docker启动情况

1
2
3
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65b2b644555b mysql:5.7 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp test_docker_mysql

到这已经可以使用docker的mysql容器提供的服务了,由于本地没有安装mysql客户端,所以这里用python去连接:

1
2
3
4
5
6
7
8
9
10
11
12
13
#coding:utf-8

import pymysql

db = pymysql.connect("localhost", "root", "123456", "mysql", port=3307)
cursor = db.cursor()
sql = "select * from user"
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print(row)

db.close()

运行脚本结果:

1
2
3
4
5
$ python mysql_test.py
('localhost', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', b'', b'', b'', 0, 0, 0, 0, 'mysql_native_password', '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', 'N', datetime.datetime(2019, 5, 9, 8, 47, 32), None, 'N')
('localhost', 'mysql.session', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', b'', b'', b'', 0, 0, 0, 0, 'mysql_native_password', '*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE', 'N', datetime.datetime(2019, 5, 9, 8, 47, 20), None, 'Y')
('localhost', 'mysql.sys', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', b'', b'', b'', 0, 0, 0, 0, 'mysql_native_password', '*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE', 'N', datetime.datetime(2019, 5, 9, 8, 47, 20), None, 'Y')
('%', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', b'', b'', b'', 0, 0, 0, 0, 'mysql_native_password', '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', 'N', datetime.datetime(2019, 5, 9, 8, 47, 32), None, 'N')

使用Docker Tensorflow Serving部署模型

下载Tensorflow Serving的Docker镜像

1
2
$ sudo docker pull tensorflow/serving
$ git clone https://github.com/tensorflow/serving

设置demo模型的路径

1
$ TESTDATA="$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata"

启动Tensorflow Serving容器,打开REST API端口

1
$ sudo docker run -t -p 8501:8501 -v "$TESTDATA/saved_model_half_plus_two_cpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving

用命令行请求模型的预测API

1
2
3
4
$ curl -d '{"instances": [1.0, 2.0, 5.0]}' -X POST http://localhost:8501/v1/models/half_plus_two:predict
{ "predictions": [2.5, 3.0, 4.5] }
$ curl -d '{"inputs": 1.0}' -X POST http://localhost:8501/v1/models/half_plus_two:predict
{ "predictions": 2.5 }