Run tensorflow in a moment with docker

This article is written for international students. The original Japanese article is [here](https://qiita.com/yakigac/items/f14f12f182a564c091d0).

※This article introduce docker with Tensorflow, but there are many people they are making some frameworks on the docker. You can install various frameworks on one PC without any problems.

# Why Should We Use Docker?
Only one line will be needed to run tensorflow with Jupyter.


```bash
sudo docker run --rm -it -p 8888:8888 tensorflow/tensorflow:latest-py3
```

# Install Docker
- **Windows**  
Install [Docker Community Edition for Windows](https://store.docker.com/editions/community/docker-ce-desktop-windows)
- **Mac**  
Install [Docker Community Edition for Mac](https://store.docker.com/editions/community/docker-ce-desktop-mac)
- **Ubuntu**  
Install [Docker Community Edition for Ubuntu](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#uninstall-old-versions). You should follow official site, but you can use the following shell script either for Ubuntu16.04.

```bash
sudo apt-get update
sudo apt-get install \
  apt-transport-https \
  ca-certificates \
  curl \
  software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce
sudo docker run --rm hello-world
```


# Run Tensorflow
## Tutorial in a moment

```bash
sudo docker run --rm -it -p 8888:8888 tensorflow/tensorflow:latest-py3
```
Access to the URL on the terminal. You can see tensorflow tutorial and can run it. (To finish `Ctrl-c`）

## With your own code
You can write your own code of course. After the following commands, you can see the blank space through Jupyter. Push **new** from right side of the page. The codes will be saved on the new dir `test`.

```bash
mkdir testdir
cd testdir
sudo docker run --rm -it -p 8888:8888 -v `pwd`:/workdir -w /workdir tensorflow/tensorflow:latest-py3
# -v `pwd`:/workdir: Mount current dir (testdir) and a dir in the docker container (workdir)
# -w /workdir: Start the container from this dir
```

# With your own environment
You should search pre-built images（ex. [tensorflow](https://hub.docker.com/r/tensorflow/tensorflow/)、[python3&opencv](https://hub.docker.com/r/jjanzic/docker-python3-opencv/)）.

However, if you want install other libraries into the pre-built images,
you can inherite them and build an additional image.

You should think the following elements as
1. Dockerfile: Design drawing
2. docker image: Resources
3. docker container: The actual working unit

Many of docker images are already on [docker hub](https://hub.docker.com/). When you `docker run` with the name of images on the hub, the image will be automatically downloaded and run as a container. Tensorflow works by the trick in the previous section.

The following example is a Dockerfile to use keras with tensorflow environment. Save the file as `Dockerfile` in your own directory.

```Dockerfile:Dockerfile
FROM tensorflow/tensorflow:latest-py3
LABEL maintainer="yakigac"

RUN pip install -q keras
```

You can `docker build` a docker image from Dockerfile in the directory.

```bash
# Dockerfile → docker image
$ sudo docker build -t tensor-keras .

# Show the built image
$ sudo docker images

# docker image → docker container
$ sudo docker run --rm -it -p 8888:8888 -v $(pwd):/workdir -w /workdir tensor-keras
```


# Do I have to type these long commands at each time?
You can use this script.

```bash:run_docker.sh
#!/bin/bash
sudo docker run \
--rm -it \
-p 8888:8888 \
-v `pwd`:/workdir \
-w /workdir \
tensor-keras \
"$@"
```

Run like following.

```bash
$ ./run_docker.sh
```

In addition that, you can use python interactive shell through docker with tensorflow.

```bash
$ ./run_docker.sh python
```

# How can I use GPUs?
You should install nvidia-docker2, and use this script instead.

Save the following files in a directory.

```Dockerfile:Dockerfile
FROM tensorflow/tensorflow:latest-gpu-py3
LABEL maintainer="yakigac"

RUN pip install -q keras
```

```bash:run_docker.sh
#!/bin/bash
sudo docker run \
--rm -it \
--runtime nvidia \
-p 8888:8888 \
-v `pwd`:/workdir \
-w /workdir \
gpu-tensor-keras \
"$@"
```

And run,

```bash
# Dockerfile → docker image
$ sudo docker build -t gpu-tensor-keras .

# Run
$ ./run_docker.sh
```