更新日志
- 2020.05.22 增加了 docker 中使用 gpu 的相关文字和配图
- 2020.06.03 增加了挂载数据目录到 docker 中相关说明
- 2021.10.25 更新失效链接
需求说明/适用场景:
- python 解释器:指向服务器上 Docker 镜像中的 python 解释器
- 文件和代码:位于服务器主机,而非 Docker 镜像中
实验环境:
- PyCharm:2020.1
- 服务器:Ubuntu 16.04.4 LTS
- Docker:19.03.4, build 9013bf583a
假定 164 服务器中的 Docker 容器已配置完成。接下来,我们将详细说明如何在 Pycharm 中,将项目的 python 解释器指向 Docker 容器。
1 配置 Docker
(1)依次点击 View --> Tool Windows --> Docker.
(2)然后点击铅笔图标,开始配置 Docker。
(3) 弹出「Docker」窗口后,在如下图所示的「Engine API URL」中填入:tcp://192.168.7.164:2375,其中 2375
为服务器端 Docker 容器的端口(端口查看方法见附件)。
当看到如上图所示的「Connection successful」,则表示已成功连接到了 docker 镜像。然后点击 「OK」。
2 新建项目
「新建项目」窗口如下图所示。
3 配置该项目的 python 解释器
(1)依次点击:File --> Settings --> Project Interpreter。弹出如下窗口,然后点击「齿轮图标」,选择「add」。
(2) 在弹出的「Add Python Interpreter」窗口中,选择所需的包含 python解释器「Image name」。然后点击 「OK」。
(3)按需更改 「Python Interpreter path:」,默认值为 python
。这里我们更改为 python3
。然后点击 「OK」。
(4) 在弹出的「Settings」窗口中点击「OK」。
至此,已将 Pycharm 中该项目的 python 解释器指向了 Docker 容器。接下来,我们将对该项目的 python 源码运行环境进行配置。
4 编辑脚本
新建一名为 fibonacci_numbers.py
的源文件,输入如下内容:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import tensorflow as tf
print(sys.executable)
a, b = 0, 1
while a < 10:
print(a, end=', ')
a, b = b, a+b
# print('\n', tf.test.is_gpu_available()) # 如果使用 gpu,去掉前面的 #
5 配置服务器和文件映射关系
项目相关的配置包括三部分:
- 配置远程服务器
- 配置本地和服务器端的目录映射关系
- 配置本地文件与服务器文件间的同步方式
(1)配置远程服务器。
(2)配置本地文件与服务器文件间的映射关系。
(3)配置文件或目录上传服务器的方式。
这里我们选择「On explicit save action (Ctrl +S)」
6 上传脚本文件到服务器
当看到如下图绿色框所示的提示,则表示上传服务器成功。
7 配置脚本运行环境
接下来,我们将对 fibonacci_numbers.py
python 脚本运行环境进行配置。
(1)第一步,依次单击 Run --> Run,
弹出如下窗口, 选择编辑配置信息。
(2)第二步,开始配置远程服务器、本地工作目录、以及 Docker 容器地址。
① 假设 Docker 中的工作目录为:/home/leaf/project
,配置 「Path Mappings」如下。
② 配置 「Docker container settings」,默认情况下,其地址是指向本地的。这里我们将其更改为 164 服务器。
注意:
- 如果需要让 docker 使用 gpu,则需要在 「Environment variables]中增加
--gpus all
选项,如下图所示
- 如果要挂载服务器中的数据目录到 Docker 中,可以增加
-v 服务器目录:容器目录
。
相关路径设置如下。
③ 最终配置完成后的效果图如下。
只使用 cpu
使用 gpu(备注:由于使用 gpu 的是另外一个项目,这里可能文件路径与 cpu 有所不同)
8 运行脚本
cpu 情况下的运行结果
使用gpu 情况下的运行结果
249e02c6b22a:python -u /home/workspace/ecg_binary_classify/connectivity_test.py
/usr/local/bin/python
WARNING:tensorflow:From /home/workspace/ecg_binary_classify/connectivity_test.py:11: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2020-05-22 07:26:06.108900: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-05-22 07:26:06.119917: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 1699975000 Hz
2020-05-22 07:26:06.121030: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f8f08000b20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-22 07:26:06.121062: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-05-22 07:26:06.125891: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-05-22 07:26:06.477046: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5c191d0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-05-22 07:26:06.477119: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1080 Ti, Compute Capability 6.1
2020-05-22 07:26:06.477132: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (1): GeForce GTX 1080 Ti, Compute Capability 6.1
2020-05-22 07:26:06.479850: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:02:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1
coreClock: 1.582GHz coreCount: 28 deviceMemorySize: 10.92GiB deviceMemoryBandwidth: 451.17GiB/s
2020-05-22 07:26:06.482018: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 1 with properties:
pciBusID: 0000:82:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1
coreClock: 1.582GHz coreCount: 28 deviceMemorySize: 10.92GiB deviceMemoryBandwidth: 451.17GiB/s
2020-05-22 07:26:06.482505: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-05-22 07:26:06.486261: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-05-22 07:26:06.489556: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-05-22 07:26:06.490141: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-05-22 07:26:06.494082: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-05-22 07:26:06.496206: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-05-22 07:26:06.504214: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-05-22 07:26:06.511782: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0, 1
2020-05-22 07:26:06.511852: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-05-22 07:26:06.516582: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-22 07:26:06.516611: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0 1
2020-05-22 07:26:06.516622: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N N
2020-05-22 07:26:06.516630: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 1: N N
2020-05-22 07:26:06.522368: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 10371 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0, compute capability: 6.1)
2020-05-22 07:26:06.525368: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:1 with 10371 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:82:00.0, compute capability: 6.1)
0, 1, 1, 2, 3, 5, 8,
True
Process finished with exit code 0
当看到 True 时,则表示 TensorFlow 可以顺利调用 GPU。
9 总结
想要实现的是:
- 仅仅使用 Docker 镜像中的的 python 解释器,
- 运行时所需的数据和代码都存储在主机中。
该场景多发生在团队内有多位成员,且成员需要使用相同的大数据,而对数据建模的方式、方法,以及所需配置的环境各不相同时。
从 pycharm 的运行结果可以看到,每运行一次程序,它将会自动创建一个容器,而在运行结束后,容器会被自动删除(因为服务器上通过 docker ps -a
找不到该 ID 的容器)。
另一种常见的使用场景是:将与该项目相关的所有数据、配置文件、代码都放在容器中。该情况下的配置稍有不同,我们将在下一节进行说明。
附件
新建项目时指定解释器
在创建项目文件时,我们采用了 pychram 默认的 python 解释器配置。此时 pycharm 将在该项目下新建一个名字为 venv
的虚拟环境。如果不想创建该虚拟环境,可以在创建项目时,自行指定 python 解释器。需要说明的是,如果此时将解释器指向 Docker 容器是行不通的,如下图所示。
如何查看服务器上 Docker 容器配置信息中的端口?
$ vim /lib/systemd/system/docker.service
参考资料
[1] 使用Pycharm远程连接及管理Docker
[2] Pycharm配置Docker解释器
[3] PY => Pycharm连接Docker及代码自动上传与运行