Docker uses containers to create virtual environments that isolate a TensorFlow installation from the rest of the system. TensorFlow programs are run within this virtual environment that can share resources with its host machine (access directories, use the GPU, connect to the Internet, etc.). The TensorFlow Docker images are tested for each release.
TensorFlow Docker requirements
Download a TensorFlow Docker image
|The latest release of TensorFlow CPU binary image. Default.|
|Nightly builds of the TensorFlow image. (unstable)|
|Specify the version of the TensorFlow binary image, for example: 1.11|
|The specified tag release and the source code.|
|The specified tag release with GPU support. (See below)|
|The specified tag release with Python 3 support.|
|The specified tag release with GPU and Python 3 support.|
|The specified tag release with Python 3 support and the source code.|
|The specified tag release with GPU support and the source code.|
|The specified tag release with GPU and Python 3 support, and the source code.|
For example, the following downloads TensorFlow release images to your machine:
docker pull tensorflow/tensorflow # latest stable release
docker pull tensorflow/tensorflow:nightly-devel-gpu # nightly dev release w/ GPU support
Start a TensorFlow Docker container
To start a TensorFlow-configured container, use the following command form:
docker run [-it] [--rm] [-p hostPort:containerPort] tensorflow/tensorflow[:tag] [command]
For details, see the docker run reference.
Examples using CPU-only images
Let's verify the TensorFlow installation using the
latest tagged image. Docker
downloads a new TensorFlow image the first time it is run:
docker run -it --rm tensorflow/tensorflow \ python -c "import tensorflow as tf; print(tf.__version__)"
Let's demonstrate some more TensorFlow Docker recipes. Start a
session within a TensorFlow-configured container:
docker run -it tensorflow/tensorflow bash
Within the container, you can start a
python session and import TensorFlow.
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):
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 with Python 3 support:
docker run -it -p 8888:8888 tensorflow/tensorflow:nightly-py3
Follow the instructions and open the URL in your host web browser:
Docker is the easiest way to run TensorFlow on a GPU since the host machine only requires the NVIDIA® driver (the NVIDIA® CUDA® Toolkit is not required).
Check if a GPU is available:
lspci | grep -i nvidia
docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
Examples using GPU-enabled images
Download and run a GPU-enabled TensorFlow image (may take a few minutes):
docker run --runtime=nvidia -it --rm tensorflow/tensorflow:latest-gpu \ python -c "import tensorflow as tf; print(tf.contrib.eager.num_gpus())"
It can take a while to set up the GPU-enabled image. If repeatably 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:
docker run --runtime=nvidia -it tensorflow/tensorflow:latest-gpu bash