Docker 安裝tensorflow

 

安裝DOCKERpython

1. https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/linux

nstall from a package

If you cannot use Docker’s repository to install Docker CE, you can download the .deb file for your release and install it manually. You will need to download a new file each time you want to upgrade Docker CE.git

  1. Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version, browse to pool/stable/ and choose amd64,armhf, or s390x. Download the .deb file for the Docker version you want to install.github

    Note: To install an edge package, change the word stable in the URL to edgeLearn about stable and edge channels.web

  2. Install Docker CE, changing the path below to the path where you downloaded the Docker package.sql

    $ sudo dpkg -i /path/to/package.deb 

    The Docker daemon starts automatically.docker

  3. Verify that Docker CE is installed correctly by running the hello-world image.shell

    $ sudo docker run hello-world 

    This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.flask

Docker CE is installed and running. You need to use sudo to run Docker commands. Continue to Post-installation steps for Linux to allow non-privileged users to run Docker commands and for other optional configuration steps.ubuntu

2. https://github.com/widuu/chinese_docker/blob/master/installation/ubuntu.md#Ubuntu%E5%AE%89%E8%A3%85Docker

##Ubuntu Docker可選配置

這部分主要介紹了 Docker 的可選配置項,使用這些配置可以讓 Docker 在 Ubuntu 上更好的工做。

  • 建立 Docker 用戶組
  • 調整內存和交換空間(swap accounting)
  • 啓用防火牆的端口轉發(UFW)
  • 爲 Docker 配置DNS服務

###建立 Docker 用戶組

docker 進程經過監聽一個 Unix Socket 來替代 TCP 端口。在默認狀況下,docker 的 Unix Socket屬於root用戶,固然其餘用戶可使用sudo方式來訪問。由於這個緣由, docker 進程就一直是root用戶運行的。

爲了在使用 docker 命令的時候前邊再也不加sudo,咱們須要建立一個叫 docker 的用戶組,而且爲用戶組添加用戶。而後在 docker 進程啓動的時候,咱們的 docker 羣組有了 Unix Socket 的全部權,能夠對 Socket 文件進行讀寫。

注意:docker 羣組就至關於root用戶。有關係統安全影響的細節,請查看 Docker 進程表面攻擊細節

建立 docker 用戶組並添加用戶

  1. 使用具備sudo權限的用戶來登陸你的Ubuntu。

    在這過程當中,咱們假設你已經登陸了Ubuntu。

  2. 建立 docker 用戶組並添加用戶。

    $ sudo usermod -aG docker ubuntu
  3. 註銷登陸並從新登陸

    這裏要確保你運行用戶的權限。

  4. 驗證 docker 用戶不使用 sudo 命令開執行 Docker

    $ docker run hello-world

建立用戶組docker,能夠避免使用sudo 
將docker和wxl(王小雷用戶名,在建立主機時默認用戶名稱是ubuntu)添加到一個組內

#默認是ubuntu用戶 #wxl@wxl-pc:~$ sudo usermod -aG docker ubuntu # 將wxl的用戶添加到docker用戶組中,若是多個用戶須要用空格隔開 如 wxl wxl1 wxl2用戶 wxl@wxl-pc:~$ sudo usermod -aG docker wxl

 

注意須要從新啓動計算機或者註銷用戶再登入,才能生效。這樣就不須要使用sudo命令了。 
那麼,如何將wxl從docker用戶組移除? 
sudo gpasswd -d wxl docker 
如何刪除剛纔建立的docker用戶組? 
sudo groupdel docker 
如何建立和刪除新用戶,如用戶newuser 
sudo adduser newuser 
sudo userdel newuser

###調整內存和交換空間(swap accounting)

當咱們使用 Docker 運行一個鏡像的時候,咱們可能會看到以下的信息提示:

WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
kernel does not support swap limit capabilities. Limitation discarded.、

爲了防止以上錯誤信息提示的出現,咱們須要在系統中啓用內存和交換空間。咱們須要修改系統的 GUN GRUB (GNU GRand Unified Bootloader) 來啓用內存和交換空間。開啓方法以下:

  1. 使用具備sudo權限的用戶來登陸你的Ubuntu。

  2. 編輯 /etc/default/grub 文件

  3. 設置 GRUB_CMDLINE_LINUX 的值以下:

    GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
  4. 保存和關閉文件

  5. 更新 GRUB

    $ sudo update-grub
  6. 重啓你的系統。

1.5.如何更新Docker

wxl@wxl-pc:~$ sudo apt-get upgrade docker-engine

1.6.如何卸載Docker

wxl@wxl-pc:~$ sudo apt-get purge docker-engine

2.運行一個web應用–Python Flask

2.1.docker簡單命令彙總以下:

  • docker run ubuntu /bin/echo 「hello world」 -運行ubuntu鏡像而且在命令窗口輸出」hello world」
  • docker run -t -i ubuntu /bin/bash -進入ubuntu這個鏡像的bash命令窗口,能夠操做本鏡像ubuntu的命令如ls
  • docker ps - 列出當前運行的容器
  • docker logs - 展現容器的標準的輸出(好比hello world)
  • docker stop - 中止正在運行的容器
  • docker version -能夠查看守護的進程,docker版本以及go版本(docker自己是用go語言寫的)

總結,能夠看出docker的命令通常爲 
[sudo] docker [subcommand] [flags] [arguments] 
如docker run -i -t ubuntu /bin/bash

3.GPU需+額外:https://medium.com/@gooshan/for-those-who-had-trouble-in-past-months-of-getting-google-s-tensorflow-to-work-inside-a-docker-9ec7a4df945b

https://github.com/NVIDIA/nvidia-docker

 

4. 安裝nvidia-docker

http://blog.csdn.net/u011987514/article/details/70943378

 

 

 

 

[plain]  view plain  copy
 
  1. # Install nvidia-docker and nvidia-docker-plugin  
  2. wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb  
  3. sudo dpkg -i /tmp/nvidia-docker*.deb && rm /tmp/nvidia-docker*.deb  
官方提供的測試方法須要下載一個1G左右的鏡像才能測試
這裏只須要輸入sudo nvidia_docker info測試一下便可 
默認狀況下Docker會把鏡像安裝在根目錄下/var/lib/docker,這樣鏡像會大量佔用系統盤空間,最終致使磁盤資源不足 
解決方案是修改默認安裝目錄
因爲個人/home磁盤資源比較多,因此都安裝到/home去

 

[plain]  view plain  copy
 
  1. zcw@ubuntu:~# mkdir docker  
  2. zcw@ubuntu:~# vim /etc/default/docker  
添加配置信息
[plain]  view plain  copy

DOCKER_OPTS="--graph=/home/docker"  

保存退出
[plain]  view plain  copy

service docker restart  

發現配置並無生效
解決方案:
[plain]  view plain  copy
  1. zcw@ubuntu:~# mkdir -p /etc/systemd/system/docker.service.d  
  2. zcw@ubuntu:~# cat /etc/systemd/system/docker.service.d/Using_Environment_File.conf  
若是沒有該文件則自行建立,添加如下內容

 [plain] view plain copy

 [Service]  
  1. EnvironmentFile=-/etc/default/docker  
  2. ExecStart=  
  3. ExecStart=/usr/bin/docker daemon -H fd:// $DOCKER_OPTS  
載入配置重啓服務
[plain]  view plain  copy
  1. zcw@ubuntu:~# systemctl daemon-reload  
  2. zcw@ubuntu:~# service docker restart  
查看配置是否生效
[plain]  view plain  copy
  1. zcw@ubuntu:~# ps -ef|grep docker  

 

 You need at least a 384.xx series driver for CUDA 9. NVIDIA recommends 384.81 or later. If you're installing from deb packages, note that the package name is different for nvidia-384 versus nvidia-375 in order that the major driver version upgrade is intentional rather than automatic.

Alternatively, you can also use a CUDA 8.0 image and not upgrade your driver:

nvidia-docker run --rm nvidia/cuda:8.0-devel nvidia-smi

3. Install Docker and nvidia-docker

# Install docker
curl -sSL https://get.docker.com/ | sh 
The docker container needs access to the GPU devices. For this purpose use `nvidia-docker` which is a wrapper around the standard `docker` command.
# Install nvidia-docker and nvidia-docker-plugin
wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.0-rc.3/nvidia-docker_1.0.0.rc.3-1_amd64.deb
sudo dpkg -i /tmp/nvidia-docker*.deb && rm /tmp/nvidia-docker*.deb
# Test nvidia-smi.
nvidia-docker run --rm nvidia/cuda nvidia-smi

You might need to use `nvidia-docker` with sudo! 

安裝Tensorflow

http://blog.csdn.net/cq361106306/article/details/54094517

 http://blog.csdn.net/freewebsys/article/details/70237003

http://blog.csdn.net/dream_an/article/details/55520205

下載tensorflow-gpu版本的源

docker pull daocloud.io/daocloud/tensorflow:0.11.0-gpu

先查下你有幾個GPU設備

[root@XXX ~]# ls -la /dev | grep nvidia crw-rw-rw-. 1 root root 195, 0 Sep 16 13:49 nvidia0 crw-rw-rw-. 1 root root 195, 255 Sep 16 13:49 nvidiactl crw-rw-rw-. 1 root root 247, 0 Sep 16 13:54 nvidia-uvm

而後再查你的docker鏡像

y@y:~$ sudo docker images [sudo] password for y: REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/daocloud/tensorflow 0.11.0-gpu dd645f420f1d 8 weeks ago 2.713 GB daocloud.io/daocloud/tensorflow 0.10.0-devel-gpu fa886c09638d 3 months ago 5.014 GB hello-world 

而後就能夠啓動咯

sudo docker run -ti -v /home/:/mnt/home --privileged=true --device /dev/nvidia0:/dev/nvidia0 --device /dev/nvidiactl:/dev/nvidiactl --device /dev/nvidia-uvm:/dev/nvidia-uvm daocloud.io/daocloud/tensorflow:0.11.0-gpu /bin/bash

上面這句有點長把它寫到docker.sh文件,而後

sh docker.sh

完成。 上面的意思是把本地的/home映射到docker的/mnt目錄 
以及各類顯卡設備也映射進去

 

主機保存鏡像爲新版本

sudo docker ps -l y@y:~$ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1f2ac36a2c9 daocloud.io/daocloud/tensorflow:0.11.0-gpu "/bin/bash" 10 minutes ago Up 10 minutes 6006/tcp, 8888/tcp 

把a1f2ac36a2c9這個名字記住 
而後

docker commit a1f2ac36a2c9 新名字

OK了

1. https://medium.com/@gooshan/for-those-who-had-trouble-in-past-months-of-getting-google-s-tensorflow-to-work-inside-a-docker-9ec7a4df945b

4. Run a Tensorflow GPU-enable Docker container 

The container itself is started as pointed out in the official documentation as follows:

# Run container
nvidia-docker run -d --name <some name> -p 8888:8888 -p 6006:6006 gcr.io/tensorflow/tensorflow:latest-gpu
# Log in
nvidia-docker exec -it <some name> bash

e.g.:

nvidia-docker run -d --name tf1 -p 8888:8888 -p 6006:6006 gcr.io/tensorflow/tensorflow:latest-gpu
nvidia-docker exec -it tf1 bash 

Note: Port 8888 is for ipython notebooks and port 6006 is for TensorBoard.

You can test if everything is alright by running this Python script.

 https://www.tensorflow.org/install/install_linux#InstallingDocker

 GPU support

 

Prior to installing TensorFlow with GPU support, ensure that your system meets all NVIDIA software requirements. To launch a Docker container with NVidia GPU support, enter a command of the following format:

$ nvidia-docker run -it -p hostPort:containerPort TensorFlowGPUImage

where:

  • -p hostPort:containerPort is optional. If you plan to run TensorFlow programs from the shell, omit this option. If you plan to run TensorFlow programs as Jupyter notebooks, set both hostPort and containerPort to 8888.
  • TensorFlowGPUImage specifies the Docker container. You must specify one of the following values:
    • gcr.io/tensorflow/tensorflow:latest-gpu, which is the latest TensorFlow GPU binary image.
    • gcr.io/tensorflow/tensorflow:latest-devel-gpu, which is the latest TensorFlow GPU Binary image plus source code.
    • gcr.io/tensorflow/tensorflow:version-gpu, which is the specified version (for example, 0.12.1) of the TensorFlow GPU binary image.
    • gcr.io/tensorflow/tensorflow:version-devel-gpu, which is the specified version (for example, 0.12.1) of the TensorFlow GPU binary image plus source code.

We recommend installing one of the latest versions. For example, the following command launches the latest TensorFlow GPU binary image in a Docker container from which you can run TensorFlow programs in a shell:

$ nvidia-docker run -it gcr.io/tensorflow/tensorflow:latest-gpu bash

The following command also launches the latest TensorFlow GPU binary image in a Docker container. In this Docker container, you can run TensorFlow programs in a Jupyter notebook:

$ nvidia-docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow:latest-gpu

The following command installs an older TensorFlow version (0.12.1):

$ nvidia-docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow:0.12.1-gpu

Docker will download the TensorFlow binary image the first time you launch it. For more details see the TensorFlow docker readme.

Next Steps

You should now validate your installation.

使用:Docker

http://songhuiming.github.io/pages/2017/02/25/an-zhuang-dockerhe-tensorflow/

4. 鏡像管理

4.0. 下載鏡像

docker pull image-name

4.1. 查看本地鏡像

docker images

4.2. 查看運行的容器

查看active鏡像:  docker ps
查看全部鏡像:    docker ps -a
查看最近的鏡像:   docker ps -l

4.3. 刪除鏡像 刪除容器

docker rmi image-name

docker rm <container id>

4.4. 在repo裏搜索鏡像

docker search [image-name]

4.5. 中止鏡像

docker stop container-id

4.6. 中止並刪除全部容器

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

5.4. 退出

exit

To restart the exited container:

docker start -a -i `docker ps -q -l`
docker start start a container (requires name or ID)
-a attach to container
-i interactive mode
docker ps List containers
-q list only container IDs
-l list only last created container

5.5. How do I install new libraries in Docker?

Therre are two ways to do this:

First methodd: Modify the Dockerfile directly to install new or update your existing libraries. You will need to do a docker build after you do this. If you just want to update to a newer version of the DL framework(s), you can pass them as CLI parameter using the --build-arg tag (see for details). The framework versions are defined at the top of the Dockerfile. For example, docker build -t shmhub/dl-docker:cpu -f Dockerfile.cpu --build-arg TENSORFLOW_VERSION=1.2.0 .

Second mthodd: you can install or upgrade in the container. After it is done, exit the cocntainer and do a commit as introducte below.

5.5. 向docker image提交container change

docker commit -m "install vim wget on ubuntu" -a "author: shm" 7de2c97f7a85 shm/ubuntu_custom

這裏7de2c97f7a85是image id.也就是在bash下面看到的root@後面的id: root@7de2c97f7a85. 這時候比較先後的鏡像,就會發現commit之後多了一個鏡像

shm@ubuntu:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
tensorflow/tensorflow   latest              ea40dcc45724        2 weeks ago         1.03 GB
ubuntu                  latest              f49eec89601e        5 weeks ago         129 MB

shm@ubuntu:~$ docker commit -m "install vim wget on ubuntu" -a "author: shm" 7de2c97f7a85 shm/ubuntu_custom
sha256:5ed742f690e11c65db83936847c7c5659c5834f6b2c93b52d110455936e6a224

shm@ubuntu:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
shm/ubuntu_custom       latest              5ed742f690e1        12 seconds ago      647 MB
tensorflow/tensorflow   latest              ea40dcc45724        2 weeks ago         1.03 GB
ubuntu                  latest              f49eec89601e        5 weeks ago         129 MB

5.6. 向倉庫提交鏡像

首先登錄

docker login -u docker-username

而後push

5.6.1. list the image and get the tag id

shm@shm-xps9550:~/projects/dl_lessons/courses-master/deeplearning1/nbs$ `docker images`
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
shmhub/dl-docker    cpu                 0f1e40d1bed8        12 days ago         9.13 GB
ubuntu              16.04               6a2f32de169d        13 days ago         117 MB

5.6.2. tag the giage with the registoryhost

docker tag 0f1e40d1bed8 pinseng/dl-docker

5.6.3. push the image to the repo

docker push docker-username/docker-image-name
docker push pinseng/dl-docker

 

4、使用docker exec進入Docker容器

除了上面幾種作法以外,docker在1.3.X版本以後還提供了一個新的命令exec用於進入容器,這種方式相對更簡單一些,下面咱們來看一下該命令的使用:

 

 

[plain]  view plain  copy
 
  1. $ sudo docker exec --help  

 

接下來咱們使用該命令進入一個已經在運行的容器

 

[plain]  view plain  copy
 
  1. $ sudo docker ps  
  2. $ sudo docker exec -it 775c7c9ee1e1 /bin/bash  

 

如何獲取localhost的地址? 
打開一個新的terminal,查看container的地址:

sudo docker inspect clever_bohr | grep IPAddress
  • 1

這裏的clever_bohr爲該正在運行的container的名字,例子以下

這裏寫圖片描述 
在瀏覽器中輸入:172.17.0.6:8888

http://blog.csdn.net/tina_ttl/article/details/51417358

http://[all ip addresses on your system]:8888/==========================>http://172.17.0.2:8888/

 

 

 

=========================================================================================================================================================================================================2017/11/8更=====

docker pull tensorflow/tensorflow:nightly-gpu-py3

 

 

======================================docker run bash版本========================================

 

===========================================================本地ipython3 notebook=================================================

 

 

=======================================================make   password==================================================

 

========================================================use docker-tf jupyter   .sh       code========================================================

相關文章
相關標籤/搜索