RUN_IN_DOCKER.md 6.4 KB
Newer Older
1 2
# How to run PaddleServing in Docker

B
barrierye 已提交
3
([简体中文](RUN_IN_DOCKER_CN.md)|English)
B
fix doc  
barrierye 已提交
4

5 6 7 8 9 10 11 12 13 14 15 16 17
## Requirements

Docker (GPU version requires nvidia-docker to be installed on the GPU machine)

## CPU

### Get docker image

You can get images in two ways:

1. Pull image directly

   ```bash
18
   docker pull hub.baidubce.com/paddlepaddle/serving:0.2.0
19 20 21 22
   ```

2. Building image based on dockerfile

B
barrierye 已提交
23
   Create a new folder and copy [Dockerfile](../tools/Dockerfile) to this folder, and run the following command:
24 25

   ```bash
26
   docker build -t hub.baidubce.com/paddlepaddle/serving:0.2.0 .
27 28 29 30 31
   ```

### Create container

```bash
32
docker run -p 9292:9292 --name test -dit hub.baidubce.com/paddlepaddle/serving:0.2.0
33 34 35 36 37 38 39
docker exec -it test bash
```

The `-p` option is to map the `9292` port of the container to the `9292` port of the host.

### Install PaddleServing

40
In order to make the image smaller, the PaddleServing package is not installed in the image. You can run the following command to install it:
41 42 43 44 45

```bash
pip install paddle-serving-server
```

46 47 48 49 50 51
You may need to use a domestic mirror source (in China, you can use the Tsinghua mirror source of the following example) to speed up the download:

```shell
pip install paddle-serving-server -i https://pypi.tuna.tsinghua.edu.cn/simple
```

52 53
### Test example

B
barrierye 已提交
54 55 56 57 58 59
Before running the GPU version of the Server side code, you need to set the `CUDA_VISIBLE_DEVICES` environment variable to specify which GPUs the prediction service uses. The following example specifies two GPUs with indexes 0 and 1:

```bash
export CUDA_VISIBLE_DEVICES=0,1
```

60 61 62 63 64 65 66 67 68 69 70 71
Get the trained Boston house price prediction model by the following command:

```bash
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/uci_housing.tar.gz
tar -xzf uci_housing.tar.gz
```

- Test HTTP service

  Running on the Server side (inside the container):

  ```bash
B
fix doc  
barrierye 已提交
72
  python -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9292 --name uci &>std.log 2>err.log &
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  ```

  Running on the Client side (inside or outside the container):

  ```bash
  curl -H "Content-Type:application/json" -X POST -d '{"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
  ```

- Test RPC service

  Running on the Server side (inside the container):

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

  Running following Python code on the Client side (inside or outside the container, The `paddle-serving-client` package needs to be installed):

  ```bash
  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)
  ```

  

## GPU

The GPU version is basically the same as the CPU version, with only some differences in interface naming (GPU version requires nvidia-docker to be installed on the GPU machine).

### Get docker image

You can also get images in two ways:

1. Pull image directly

   ```bash
116
   nvidia-docker pull hub.baidubce.com/paddlepaddle/serving:0.2.0-gpu
117 118 119 120
   ```

2. Building image based on dockerfile

B
barrierye 已提交
121
   Create a new folder and copy [Dockerfile.gpu](../tools/Dockerfile.gpu) to this folder, and run the following command:
122 123

   ```bash
124
   nvidia-docker build -t hub.baidubce.com/paddlepaddle/serving:0.2.0-gpu .
125 126 127 128 129
   ```

### Create container

```bash
130
nvidia-docker run -p 9292:9292 --name test -dit hub.baidubce.com/paddlepaddle/serving:0.2.0-gpu
131 132 133 134 135 136 137 138 139 140 141 142 143
nvidia-docker exec -it test bash
```

The `-p` option is to map the `9292` port of the container to the `9292` port of the host.

### Install PaddleServing

In order to make the image smaller, the PaddleServing package is not installed in the image. You can run the following command to install it:

```bash
pip install paddle-serving-server-gpu
```

144 145 146
You may need to use a domestic mirror source (in China, you can use the Tsinghua mirror source of the following example) to speed up the download:

```shell
B
barrierye 已提交
147
pip install paddle-serving-server-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple
148 149
```

150 151
### Test example

B
barrierye 已提交
152 153 154 155 156
When running the GPU Server, you need to set the GPUs used by the prediction service through the `--gpu_ids` option, and the CPU is used by default. An error will be reported when the value of `--gpu_ids` exceeds the environment variable `CUDA_VISIBLE_DEVICES`. The following example specifies to use a GPU with index 0:
```shell
export CUDA_VISIBLE_DEVICES=0,1
python -m paddle_serving_server_gpu.serve --model uci_housing_model --port 9292 --gpu_ids 0
```
B
barrierye 已提交
157 158


159 160 161 162 163 164 165 166 167 168 169 170
Get the trained Boston house price prediction model by the following command:

```bash
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/uci_housing.tar.gz
tar -xzf uci_housing.tar.gz
```

- Test HTTP service

  Running on the Server side (inside the container):

  ```bash
B
barrierye 已提交
171
  python -m paddle_serving_server_gpu.serve --model uci_housing_model --thread 10 --port 9292 --name uci --gpu_ids 0
172 173 174 175 176 177 178 179 180 181 182 183 184
  ```

  Running on the Client side (inside or outside the container):

  ```bash
  curl -H "Content-Type:application/json" -X POST -d '{"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
  ```

- Test RPC service

  Running on the Server side (inside the container):

  ```bash
B
barrierye 已提交
185
  python -m paddle_serving_server_gpu.serve --model uci_housing_model --thread 10 --port 9292 --gpu_ids 0
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  ```

  Running following Python code on the Client side (inside or outside the container, The `paddle-serving-client` package needs to be installed):

  ```bash
  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)
  ```

B
barrierye 已提交
202 203 204 205 206



## Attention

B
fix doc  
barrierye 已提交
207
The images provided by this document are all runtime images, which do not support compilation. If you want to compile from source, refer to [COMPILE](COMPILE.md).