LoginSignup
0

More than 3 years have passed since last update.

Run tensorflow in a moment with docker

Last updated at Posted at 2018-01-31

This article is written for international students. The original Japanese article is here.

※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.

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

Install Docker

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

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.

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. tensorflowpython3&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. 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
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.

# 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.

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

Run like following.

$ ./run_docker.sh

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

$ ./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
FROM tensorflow/tensorflow:latest-gpu-py3
LABEL maintainer="yakigac"

RUN pip install -q keras
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,

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

# Run
$ ./run_docker.sh

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0