TensorFlow Docker requirements

Install Docker

1
2
3
sudo pacman -S docker
systemctl enable docker.service
systemctl start docker.service

For GPU support on Linux,

1
yay -S nvidia-container-toolkit

The official TensorFlow Docker images are located in the tensorflow/tensorflow Docker Hub repository.

Start a TensorFlow Docker container

1
docker run [-it] [--rm] [-p hostPort:containerPort] tensorflow/tensorflow[:tag] [command]

Examples using CPU-only images

1
docker pull tensorflow/tensorflow 
1
2
docker run -it --rm tensorflow/tensorflow \
   python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Start a bash shell session within a TensorFlow-configured container:

1
docker run -it tensorflow/tensorflow bash

To run a TensorFlow program developed on the host machine within a container, mount the host directory and change the container’s working directory (-v hostDir:containerDir -w workDir):

1
docker run -it --rm -v $PWD:/tmp -w /tmp tensorflow/tensorflow python ./script.py

Permission issues can arise when files created within a container are exposed to the host. It’s usually best to edit files on the host system.

Start a Jupyter Notebook server using TensorFlow’s nightly build:

1
docker run -it -p 8888:8888 tensorflow/tensorflow:nightly-jupyter

Follow the instructions and open the URL in your host web browser: http://127.0.0.1:8888/?token=...

1
docker pull tensorflow/tensorflow:1.15.4-py3-jupyter
1
docker run -it tensorflow/tensorflow:1.15.4-py3-jupyter bash

GPU support

Check if a GPU is available:

1
lspci | grep -i nvidia

Verify your nvidia-docker installation:

1
docker run --gpus all --rm nvidia/cuda nvidia-smi
1
docker pull tensorflow/tensorflow:1.15.4-gpu-py3

Examples using GPU-enabled images

Download and run a GPU-enabled TensorFlow image (may take a few minutes):

1
2
docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu \
   python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

It can take a while to set up the GPU-enabled image. If repeatedly running GPU-based scripts, you can use docker exec to reuse a container.

Use the latest TensorFlow GPU image to start a bash shell session in the container:

1
docker run --gpus all -it tensorflow/tensorflow:latest-gpu bash

Reference

  1. https://www.tensorflow.org/install/docker