ITPub博客

首页 > 云计算 > > Docker--深度学习环境配置一站式解决方案

Docker--深度学习环境配置一站式解决方案

作者:浪花小雨滴 时间:2020-05-09 11:37:20 0 删除 编辑

我最近有一些nmb想跟大家分享:

1. 在安装某个库的时候,明明是按官方说的一步步来,可是在我电脑上就是各种报错。

2. 直接用pip或者anaconda安装的OpenCV没法调用本地的ffmpeg编解码器,还有一些需要从头编译的库直接利用conda安装也会出现或多或少的问题。

3. 别人放在github的代码我下载到本机后无法直接跑,我只想验证一下他的结果,但是却需要各种依赖库,各种改环境变量,改变我本地的各种设置,最终系统改得连我自己都不认识了,就算跑通了它的代码,要跑另一些又TM不行了。


毕竟不是CS本专业出身,对于操作系统和软件的基础不是很牢固,所以我再折腾了一周之后,终于决定投入docker的怀抱,并且自己封装了一个image,涵盖了目前做研究需要的各种深度学习和软件库:hub.docker.com/r/yiming 希望这篇简介可以帮助大家摆脱环境的烦恼:


Docker是什么?

中文社区给了简洁的概括:

  • docker是一个开源的软件部署解决方案;
  • docker也是轻量级的应用容器框架;
  • docker可以打包、发布、运行任何的应用。

在我看来,就是类似虚拟机的东西,只是不同点是虚拟机的硬件都是靠CPU虚拟出来的,而docker可以调用你的本地硬件进行计算,而且docker的封装更为精简。

Docker中最重要的两个概念就是image和container,我从官网找到了以下解释

Docker objects

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

IMAGES

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

CONTAINERS

A container is a runnable instance of an image. You can create, run, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

A container is defined by its image as well as any configuration options you provide to it when you create or run it. When a container stops, any changes to its state that are not stored in persistent storage disappears.

简言之,就像程序和进程的区别一样,程序是一首曲子,和进程则是曲子的一次演奏。当你run一个container后,无论如何折腾当前的container都不会影响到image,当然,如果你想保存你对container的修改,请参考docker的commit命令。



Nvidia-Docker插件

Nvidia-Docker是英伟达官方出品的docker插件,专门用来允许container直接调用本地的英伟达GPU。不过目前只支持linux系统,需要在你的本机安装好CUDA和CUDNN


Docker Hub

和github一样,DockerHub是用来分享自己制作的image的平台,我做的image可以在这里找到hub.docker.com/r/yiming


使用案例

  • 运行一个container,并且开启交互式的命令行。当你运行这个命令后,就像远程登录了一台服务器一样,进入bash界面。

  • nvidia-docker run -it yiminglin/dl-image:latest


  • 载入home路径到docker的container
    需要注意的是一旦你退出某个container,所有你做的东西都不会保存,所以为了保存数据和代码,我们可以把某个目录载入到container中,这样我们在docker中对该目录所做的修改都会保存到本机了。

    nvidia-docker run -v /home/yiming/:$(pwd) -it yiminglin/dl-image:latest
    上述命令把加载了我本地的home路径,在container就可以在/home/目录中看到我本地机器上保存在home目录的所有东西:

  • 使用Jupyter notebook:
    Jupyter notebook是做实验保存数据和分析的重要工具,也是我主要用的开发工具,如果你不需要打开命令行,而是直接想使用jupyter,可以用下面的命令:

    nvidia-docker run -v /home/yiming/:$(pwd) -it -p 8888:8888 yiminglin/dl-image:latest sh -c "jupyter notebook --allow-root --no-browser --ip 0.0.0.0 /home/yiming"
    该命令把本地home路径载入container,并且把本地端口8888映射到docker container的8888端口(Jupyter的默认端口),最后中home目录中打开notebook,可以看到提示如下:

    在浏览器输入它提示的网站,就可以使用notebook了

  • 验证container是否调用了GPU
    我用一个notebook代码来验证是否真的调用了GPU,首先利用
    nvidia-smi
    本地查看我的GPU状态,

    只有一些系统进程,现在我们在上一步的notebook中新建一个python2的notebook,然后运行一些简单的tensorflow代码
    简单的tensorflow测试代码
    可以看到container命令行中的输出为:

    本机的GPU状态为

    由此可以看出nvidia-docker确实实现了container到GPU之间的数据传输。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3142262/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    39893