
TensorFlow is a powerful open-source machine learning library that can be a bit tricky to set up, especially for beginners. To simplify this process, we can use Docker, a containerization platform that allows us to run TensorFlow in a isolated environment.
Docker provides a pre-built TensorFlow image that we can use to create a container, which eliminates the need to install TensorFlow manually. This image is available on Docker Hub, a registry of Docker images.
With Docker, we can create a TensorFlow environment in a matter of minutes, which is perfect for experimentation and development.
Recommended read: Does Tensorflow Automatically Use Gpu
Installation
To install Docker, you'll need to follow the instructions provided by Docker, which can be found online.
You can install Docker on your computer, but you'll need to use a specific version of Docker to work with GPUs, as Docker containers are platform and hardware agnostic.
To install the Nvidia GPU compatible Docker version, you'll need to run a few commands, including one to point to the specific installation files for GPU-compatible Docker.
Suggestion: Check If Tensorflow Is Using Gpu
You'll also need to restart Docker after installation to ensure the changes take effect.
Once you have Docker installed, you can verify that it's working by running a simple command.
On a Mac, you'll need to navigate to your Applications folder and open the Docker application before running any commands at the command line.
On Linux, you may need to preface each Docker command with "sudo" to get it working.
If you're having issues running Docker containers without sudo on Ubuntu, one fix is to run the command "sudo chmod 666 /var/run/docker.sock".
You can test your Docker installation by running a simple command.
To run GPU-accelerated Docker containers, you'll need to install Nvidia's container toolkit and then install Nvidia-docker.
Additional reading: Hubectl Run Docker Image from Azure Container Registry
Creating a
Creating a Docker container is the next step in getting TensorFlow up and running.
You'll need to create a Docker image, which is done by creating a Dockerfile in a new directory.
To start, choose a base image for your Dockerfile. You can use a text editor to create a new file named Dockerfile and add the necessary content.
To create a Docker container, you'll need to run a command using the Docker run command. You can use the -i flag to create an interactive session for yourself.
You'll need to select an image for your container, such as python:3.9-slim. This image will be used to run your container.
Here are some common Docker images you can use:
- python:3.9-slim
Once you've selected an image, you can start an interactive session by running the Docker run command with the -i flag. This will open a new terminal session inside your container.
You can name your container for easier identification. For example, you can name it "tf" to indicate that it's a TensorFlow container.
The Docker run command will download the image if it's not already on your machine. This can take a few minutes depending on your internet connection.
Related reading: Pull and Run Docker Image from Azure Container Registry
Testing and Verification
To verify that TensorFlow Docker has been installed correctly, you can check if the TensorFlow GPU has been installed properly on the machine.
We'll follow the official tutorial steps to ensure everything is set up correctly.
Testing our installation is crucial to ensure we're working with the right tools.
As it is in the official tutorial, step 6 is where we'll check if TensorFlow GPU has been installed properly on the machine.
Readers also liked: Machine Learning Crash Course with Tensorflow Apis
Packages and Setup
You may not have every library you want in the Docker container's Python environment, so you'll need to install extra packages inside your notebooks.
To work with other additional libraries or packages not included in the standard TensorFlow image, you can build a custom TensorFlow image with Docker.
A Docker image is a template containing instructions for how to create a container, and you can download the stable version of the TensorFlow Docker image by using the “docker pull” command.
You can specify the py3 tag to get python 3 (3.6 as of now) and also install the ssh-server and create a non-root user to run jupyter.
Here are some useful Docker commands:
- docker pull: downloads the Docker image, but does not run it.
- docker run: runs the Docker image, and will automatically pull the package if you try to run a package that you have not downloaded.
Setting Up
Setting up the right environment is crucial for a smooth package installation process.
You'll need to install Docker, a tool that packages applications and their dependencies in isolated environments called containers. You can find installation instructions for Docker on their official website.
To verify that your Docker installation is working, run the command `docker run -it --rm alpine /bin/sh -c "echo Hello World!"` in your terminal. If your installation is successful, you should see the message `Hello World!`.
To create a Docker container for TensorFlow, you need to download and run a Docker image from the tensorflow/tensorflow Docker Hub repository. You can download the stable version of the TensorFlow Docker image using the command `docker pull tensorflow/tensorflow:latest`.
Here are the steps to set up a Docker container for TensorFlow:
- Download the Docker image using `docker pull tensorflow/tensorflow:latest`
- Run the Docker image using `docker run -it --rm tensorflow/tensorflow:latest`
- You can also map a port and mount folders to share files between the host and the container
To run a CUDA-enabled container and test nvidia-docker, run the command `docker run -it --rm --gpus all nvidia/cuda:11.0-base-ubuntu20.04 nvidia-smi`. This will check if nvidia-docker is working correctly.
You might like: Run Docker Image from Azure Container Registry
Packages
The Python environment in a Docker container may not have every library you want. This can be a challenge, especially if you're not a fan of digging through documentation to find workarounds.
One solution is to install extra packages inside your notebooks, as demonstrated by installing something like a specific library.

Docker is a program that lets us virtualize Linux machines and other types of machines in our Mac or Windows. This means we can run multiple environments on a single machine without conflicts.
You can install packages like TensorFlow inside your notebooks, which can be useful if the Docker container's Python environment is missing a necessary library.
Featured Images: pexels.com


