Play TensorFlow on Windows (a)--install Docker "turn"

Source: Internet
Author: User
Tags docker run docker toolbox

"Google" + "deep learning", two tags let the December 2015 Google open-source deep learning tool TensorFlow after its release quickly became the world's hottest open source project, April 2016, open source TensorFlow support distributed features, The application to the production environment is further.

The TensorFlow API supports Python 2.7 and Python 3.3+, with a total of 4 installation options.

    • Pip Install
    • Virtualenv Install
    • Anaconda Install
    • Docker Install

Most of these support Linux and Mac OS, and since the main development environment is Windows, I chose the most flexible Docker way to install TensorFlow. TensorFlow also has a GPU-supported version, this article explores only the cpu-only version.

I plan to complete a series of 3 blog posts, the first step to install Docker, the second step of the single version of TensorFlow demo, the third step distributed TensorFlow demo, to achieve within 1 months.

1 What is Docker?

Borrow the largest line of the Docker website.

Docker allows a application with all of its dependencies into a standardized unit for software development .

Functionally, Docker can also be understood as a virtualized solution that can be used to quickly deploy a development environment by building mirrors that contain different software.

Then borrow a picture of the official website, the blue part of the left from the kernel began a layer of Debian, Emacs, Apache formed an image, each layer is read-only, we run this image, the top cover a layer can read and write container, Let's do some editing and modification, a simple server can be used, as well as the orange-red part of the right side, kernel the above with BusyBox to form an image, after running it can be in a very lightweight way to run the commands supported in BusyBox. And then there's the tensorflow image that we're going to learn, which includes all the dependencies needed to run it, and a simple operation to build the TensorFlow development environment.

More Docker understanding recommended Reading 10 images take you in-depth understanding of Docker containers and mirrors

2 Installing Docker

To run Docker on Windows, you first need to download and install Docker Toolbox. By the way, the Docker Windows documentation, prefer official documents can be seen here, you can follow the steps in this article to continue.

2.1 Confirm the system version

First confirm that your system is a win 7 or newer 64-bit system, and you need to support hardware virtualization technology.

Win 8 and above view method:

Win 7 View Method

Win 7 running Microsoft? The Hardware-assisted Virtualization Detection tool performs a check.

Once you're done, you can move on to the next step.

2.2 Installing Docker Toolbox

Click Docker Toolbox Download, the latest version of this article is 1.11.1.
The installation process installs the various components of Docker and Oracle VirtualBox, because Docker relies on some features of the Linux kernel, so both Mac and Windows need to run a small Linux system on the machine as the host system. If you have installed the latest version of VirtualBox, you do not need to reinstall it.

Choose your own installation path, if you have already installed Git can also remove the tool check, VirtualBox is the same, others can go all the way next.

2.3 Configuring Docker

After installation, it is recommended to configure an environment variable Machine_storage_path, which defines where the virtual machine is saved, because more and more images are placed in the virtual disk files of the VM, and the virtual disk files will become larger and bigger. Placed in the default C-drive user directory may cause some trouble later, refer to, set to any location you like.

After the setup is complete, win+r enter CMD to open a Windows command-line window.

Enter the following command to create a new virtual machine ron-docker, using VirtualBox as the driver, this virtual opportunity is automatically created in the directory of your Machine_storage_path configuration, The installation process pulls Boot2docker.iso from GitHub, may be slow, can be downloaded manually, or you can copy Boot2docker.iso from the Toolbox installation directory directly to%machine_storage_path%/ The cache.

Docker-machine Create ron-docker-d VirtualBox

 

Also note that there may be some windows in the process that require permissions to be executed by the window, some of which are minimized on the taskbar, and one by one are granted permissions.

After the setup is complete, type to docker-machine ls view the newly created virtual machine, and the following command will take Ron-docker as an example , please follow your own configuration to modify the reader.

C:\users\ron>docker-machine lsname         ACTIVE   DRIVER       State     URL                         SWARM   Docker    Errorsron-docker   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.1

The cmd we use is not yet connected to the Docker engine and can be docker-machine env mydocker configured by looking at how it is set up.

C:\users\ron>docker-machine Env Ron-dockerSET docker_tls_verify=1set docker_host=tcp://192.168.99.100:2376set Docker_cert_path=e:\virtualbox VM \machines\ron-dockerSET docker_machine_name=ron-dockerrem Run this command to Configure your Shell:rem @FOR /F "tokens=*" %i in (' docker-machine env ron-docker ') do @%i
                                

Type the following command to complete the environment variable configuration

FOR /F "tokens=*" %i IN (‘docker-machine env ron-docker‘) DO %i
3 Running the image

Below we download the latest TensorFlow image to experience the image running under Docker.

3.1 Download image
docker pull gcr.io/tensorflow/tensorflowdocker imagesREPOSITORY                     TAG                 IMAGE ID            CREATED             SIZEgcr.io/tensorflow/tensorflow   latest              aeff5a9860a3        2 weeks ago         714.2 MB
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Of course, there may be network problems, please according to their own specific situation to solve their own, hanging agent, VPN, manual download can be.

3.2 Start mirroring and explore

Use the following command to start mirroring.

run -it gcr.io/tensorflow/tensorflow bash
    • 1

Option-I is used to keep stdin on the current window,-T is used to assign a pesudo-tty, and two options allow the current window to run like a Linux bash without the slightest sense of vainly disobey. The first parameter specifies the image to use, the second parameter specifies the command that is enabled after starting the image, and launching with Bash allows us to begin our first exploration.

    • Uname-a, View information
    • CD to/directory, view directory structure
    • Run TensorFlow's Hello World
[Email protected]:/# Uname-alinux ec9bfd2760824.4.8-boot2docker#1 SMP Mon Apr 21:57:27 UTC x86_64 x86_64 x86_64 gnu/linux[email protected]:/# Ls/bin Dev home lib64 mnt opt root run_jupyter.sh srv tmp Varboot etc lib Media notebooks proc run sbin sys usr[email p rotected]:/# Pythonpython2.7.6 (default, June 22 2015, 17:58:13" [GCC 4.8.< Span class= "Hljs-number" >2] on Linux2type  "help",  "copyright", Span class= "hljs-string" > "credits" or  "license" for more Information.>>> import TensorFlow as tf>>> hello = tf.constant ( ' Hello, Tensorflow! ') >>> sess =TF. Session () >>> sess.run (hello)  ' Hello, tensorflow! ' >>> a = tf.constant (10) >>> B = tf.constant (32) >>> Sess.run (a+b) 42>>>       

Note the Run_ under/directory Jupyter.sh, this is actually the default command that is started by the current version of TensorFlow, which means that if we do not specify bash when booting the image, the script will run by default, unlike some earlier versions of TensorFlow, which are not mentioned in many tutorials and can be confusing for readers to Try docker run -it gcr.io/tensorflow/tensorflow it, it will start a notebook service, run on the local 8888 port, but it is not possible to open notebook from Windows browser, which is related to the mechanism of Docker itself and the two reasons for running on virtual If the reader wants to see Notebook now, click here to take you to the Port forwarding section to complete more necessary configuration.

4 Configure your own Quick Launch window

Recalling the above procedure, it takes 3 steps to turn a Windows CMD window into a window that runs a specific container.

    1. Start a virtual machine
    2. Configuring environment variables for virtual machines
    3. Run mirror

The following I use a startup script, you need to modify the corresponding machine-name, and then save the script as My-start.bat, placed in the Toolbox installation directory. After you right-my-start.bat-> to the desktop shortcut, configure the shortcut by the diagram below, and the/k parameter will not automatically close after the bat finishes running. This allows you to double-click Run to get a new CMD window that can run the image.

@ECHO offSET Machine-name=ron-dockerECHO Init ...for/f%%iin (' docker-machine status %machine-name% ') do set status=%%i if %status%==running (ECHO %machine-name% is running.) else (echo %machine-name% is shutdown . Docker-machine start %machine-name%) echo Configuring Environment ... for/f "tokens=*" %%i IN (' Docker-machine Env %machine-name% ') do %%iecho Init finished.             

5 Complete more necessary configuration

Above we completed the process of mirroring from download to launch, but before using Docker as a development environment, there are some very necessary configuration work to do, let's complete the following 3 steps together.

5.1 Port forwarding

Our container runs on a small Linux virtual machine, and the virtual machine runs on the VirtualBox of the Windows system, and naturally, the service running on the container is no longer open to the browser on Windows to access directly. Port forwarding is to solve this problem, a total of two steps:

Establish port forwarding between windows and virtual machines

You can do this through the VirtualBox management interface, and then restart the virtual machine.

Configure port forwarding between a virtual machine and a container

Use the-P option

run -it -p 8888:8888 gcr.io/tensorflow/tensorflow

After you type this command, open your browser, access localhost:8888, and see the notebook service running in the container.

5.2 Disk Mapping

Disk mapping is a very useful one-step configuration that maps disks on Windows directly into containers, so that you can develop code on Windows to run directly in the container, avoiding the big hassle of writing a copy. Disk mapping is handled in a similar way to port forwarding, where the mapping of a disk requires the completion of a two-step configuration of Windwos to a virtual machine to a container, follow these 3 steps:

Establish disk mapping between windows and virtual machines

After you open the Settings page for the virtual machine, set the folder you want to share and its name in the shared folder, and restart the virtual machine.

Mount a disk in a virtual machine

This step uses the name you just set, and here I am Docker.

mkdir -p /home/docker/datamount -t vboxsf -o uid=1000,gid=50 docker /home/docker/data

Here, the changes we make in Windows can be reflected in real time in the virtual machine.

Establishing a disk mapping between a virtual machine and a container

Use the-V option to establish a mapping relationship of two directories

-it -v /home/docker/data:/data gcr.io/tensorflow/tensorflow bash

All right, try adding a Python Hello world to your Windows shared folder and try it out in Docker python /data/hello.py .

5.3 Configuring Startup Scripts

We created a virtual machine above with Docker-machine, most of its directories will be restored after the restart, in addition to/MNT/SDA1, this directory is the virtual machine virtual disk File DISK.VMDK The location of the mount, we can modify the inside of the /mnt/sda1 /var/lib/boot2docker/profile file, at the end of the file to add our custom Start command, such as adding the previous disk mapping, is to add the following two sentences at the end of the profile file.

mkdir -p /home/docker/datamount -t vboxsf -o uid=1000,gid=50 docker /home/docker/data

This completes the disk mount every time the virtual machine starts.

6 conclusion

To get here, the work of installing Docker before using TensorFlow I do my best to describe in the most detail, if you can see here, I really feel very honored, the first time to write such a long tutorial, I hope to bring some help, finally, thank the following reference contributors, Please look forward to the second chapter of TensorFlow Trilogy ~

7 References
    1. Building a development environment with Docker: http://tech.uc.cn/?p=2726
    2. 10 pictures take you in-depth understanding of Docker containers and mirrors: http://dockone.io/article/783
    3. How to install and run TensorFlow on a Windows pc:http://www.netinstructions.com/ how-to-install-and-run-tensorflow-on-a-windows-pc/
    4. Install Docker for windows:https://docs.docker.com/windows/
    5. 5 useful Docker Tips and Tricks on windows:http://blog.pavelsklenar.com/5-useful-docker-tip-and-tricks-on-windows/

Play TensorFlow on Windows (a)--install Docker "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.