Ubuntu基于Docker搭建TensorFlow+Pycharm环境

交代背景:刚刚重装了ubuntu 18.04,现在立刻装docker(待编辑不要看)

Setup
Prerequisites

Ubuntu (preferably 16.04 or up) . CentOS is also supported but may need some extra settings.
Nvidia GPU (optional but strongly recommended)
Docker Hub account
Make sure your account has root access. Refer to here for instructions on how to grant sudo access.

Setup Docker Environment on Ubuntu host

Run the following script as a file with bash under sudo, or use this file directlly.(You can skip this step if you’ve had it setup already).

Install Nvidia Driver

You can install nvidia driver by running the shell script as follows or you can also install driver in Software&Updates -> Additional Drivers

#!/bin/bash

Install Nvidia Driver

这下面两步是必须操作,第三部NVIDIA-390先不装
add-apt-repository ppa:graphics-drivers/ppa
apt-get update && apt-get upgrade -y
apt install -y nvidia-390 nvidia-modprobe

[点击并拖拽以移动]

好了之后就去软件和更新里面找到附加驱动,搜索到好多驱动,因为上次我用的环境就是410显卡驱动,所以这次还是选410(因为驱动环境一定要装,不用把显卡驱动在docker里面装)
Install Docker

下面就是docker从国外下要么选中科大的

Install Docker From Official Website

apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
apt-get update
apt-get install docker-ce

You can also install it from USTC’s Mirror (recommended)

Install Docker From USTC’s Mirror

apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository “deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable”
apt-get update
apt-get install docker-ce

 Install Nvidia-Docker

Install Nvidia-Docker

curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add -
distribution=$(. /etc/os-release;echo I D ID VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
apt-get update
apt-get install -y nvidia-docker2
pkill -SIGHUP dockerd

Setup Pycharm IDE

Download linux version of PyCharm Community. (or other IDE as you wish) and unzip to /dev
Use Docker

We will be using docker for easy creation and maintenance of development environment.

Docker Setup For Development

In order to make Docker environment ready for development, a few setups need to be made before actually using Docker.
To allow Docker container to access X11 server (so that it can use host’s screen to display) on host, persmissions need to be granted to local access, by adding the following line to the end of ~/.profile file (using gedit or vim to edit), so that it gets executed everytime the host machine boots:

xhost +local:root

Using Docker

To pull docker image from docker hub, you can also pull your own image

Pull docker image named deepgaze/eap-models-dev

This image contains environments such as tensorflow-gpu, pytorch, keras

sudo docker pull deepgaze/eap-models-dev:latest

我就简单一点:直接附上全部代码,桌面建一个docker_install.sh文件,然后就里面输入
apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu ( l s b r e l e a s e c s ) s t a b l e " a p t g e t u p d a t e a p t g e t i n s t a l l d o c k e r c e c u r l s L h t t p s : / / n v i d i a . g i t h u b . i o / n v i d i a d o c k e r / g p g k e y a p t k e y a d d d i s t r i b u t i o n = (lsb_release -cs) stable" apt-get update apt-get install docker-ce curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add - distribution= (. /etc/os-release;echo I D ID VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
apt-get update
apt-get install -y nvidia-docker2
pkill -SIGHUP dockerd
docker pull deepgaze/eap-models-dev:latest

接下来就是运行,终端,sudo bash 文件夹拉进去

对了至于pycharm,因为我们要建docker,所以在主目录里面新建一个dev文件,就是以后docker里面的东西都是在这个文件夹下面,也就是你把你文件夹直接拉到这里面来就可以docker里面可以看到,再建一个pycharm文件夹,以后pycharm什么东西就是放这里。
ok下面就是安装pycharm,
cd dev
bash pycharm/bin/pycharm.sh,然后就像以前一样安装就是了

对了还有要如下操作,这是为了以后虚拟docker可以看到我现在系统里面文件
gedit .profile
末尾加上xhost +local:root
To create container

#!shell script

Assume the source code is located at ~/dev/models and pycharm is located at ~/dev/pycharm on your host machine

Feel free to add additional mapping to the following command if necessary.

e.g., if some of your data are stored in ~/data, then you may add ‘-v ~/data:/root/data’ so that container has access to the data

The following script has enabled display/camera access from container as well as port mapping required for accessing tensorboard in container from host

sudo docker run -ti --runtime=nvidia -e=“DISPLAY” -e=“QT_X11_NO_MITSHM=1” --privileged -v="/tmp/.X11-unix:/tmp/.X11-unix:rw" -p 0.0.0.0:6006:6006 -p 8888:8888 --ipc=host -v /dev/video0:/dev/video0 -v ~/dev/pycharm:/root/pycharm -v ~/dev/datasets:/root/datasets -v ~/dev/models:/root/models --name models deepgaze/eap-models-dev bash

A container named ‘models’ will be created. You may want to restart and reuse this container by name so that all changes to container settings (such as Pycharm settings) persists.

You may leave the container and come back to host by

#!shell script
exit

To restart the same container by

#!shell script
sudo docker start -i models # Start container by name so that the same one will be started.

If you want to open multiple terminals to the container

#!shell script
sudo docker exec -ti models bash

Configure Pycharm Interpreter

run pycharm

#!shell script
bash pycharm/bin/pycharm.sh

[点击并拖拽以移动]

open pycharm
select file->settings->project->project interpreter
click wheel gear icon and select add
select system interpreter, then select /usr/bin/python3.5

参考博客:https://blog.csdn.net/weixin_38502181/article/details/84632610

猜你喜欢

转载自blog.csdn.net/qq_38153833/article/details/84799021
今日推荐