Docker

How to Create a TensorFlow Development Environment with Docker Compose

Docker and Docker Compose can be used to create an isolated TensorFlow development environment. You can run your TensorFlow codes in there and run any commands without fear.

In this article, we will show you how to create a TensorFlow development environment with Docker and Docker Compose. We will also show you how to access the terminal of the TensorFlow Docker container and how to run your TensorFlow codes in there.

Topic of Contents:

  1. Installing Docker
  2. Creating a Docker Compose File for the TensorFlow Docker Development Environment
  3. Accessing the Terminal of the TensorFlow Docker Container Using the Docker Compose
  4. Creating and Running a TensorFlow Program in the Docker Container
  5. Stopping the TensorFlow Docker Compose Project
  6. Conclusion

Installing Docker

To create a TensorFlow development environment with Docker and Docker Compose, you must have the Docker and Docker Compose installed on your computer.

If you need any assistance in installing Docker on your computer, check out one of the Docker installation articles at linuxhint.com.

Once Docker is installed, you should be able to access it as follows:

$ docker --version

To work and manage the Docker projects more efficiently, you also need to have the Docker Compose.

At the time of this writing, the Docker Compose is installed automatically while installing the Docker.

To make sure that you can access the Docker Compose, run the following command:

$ docker-compose --version

If, for some reason, you don’t have the Docker Compose installed, you can follow one of the Docker Compose installation articles at LinuxHint.com.

Creating a Docker Compose File for the TensorFlow Docker Development Environment

First, open a Terminal app and create a ~/projects/docker-tensorflow project directory (let’s say) with the following command:

Then, open the ~/projects/docker-tensorflow project directory (let’s say) with your favorite IDE or code editor. We are using the Visual Studio Code. We use the following command to open the project directory on the Visual Studio Code. If you’re using some other IDE or code editor, follow your own workflow.

$ code ~/projects/docker-tensorflow

Check the “Trust the authors of all files in the parent folder” option and click on “Yes, I trust the authors”.

Create a new docker-compose.yaml file in the project directory and type in the following lines of code:

version: "3"
services:
  tensorflow:
    image: tensorflow/tensorflow
    volumes:
      - .:/project:rw
    tty: true

This is how the docker-compose.yaml file looks like in the Visual Studio Code.

To open a Terminal on the Visual Studio Code, press <Ctrl> + ` or click on the Terminal > New Terminal from the menu. Then, run the following command to start the Docker Compose project:

$ docker compose up -d

The Docker Compose project should start.

NOTE: We already have the TensorFlow Docker image downloaded and cached on our computer. If you’re running this command for the first time, you will not have the TensorFlow Docker image. Docker will download and cache the TensorFlow Docker image on your computer. So, the command will take a while to complete and start the Docker Compose project.

Accessing the Terminal of the TensorFlow Docker Container Using the Docker Compose

To access the terminal of the TensorFlow Docker container that you started with Docker Compose, run the following command:

$ docker compose exec tensorflow bash

You should be connected to terminal of the TensorFlow Docker container. You can run any command that you want in here just like you would do on your computer.

Creating and Running a TensorFlow Program in the Docker Container

To test whether you can run the TensorFlow programs in the Docker container, create a new Python script which is “tensorflow-test.py” in the project directory and type in the following lines of code:

import tensorflow as tf
print("Tensorflow version: ", tf.__version__)

From the terminal of the tensorflow Docker container, navigate to the project directory /project as follows:

$ cd /project

The tensorflow-test.py file should be there.

$ ls -lh

Run the Python “tensorflow-test.py” script with the following command:

$ python tensorflow-test.py

If TensorFlow is working on the Docker container, the “tensorflow-test.py” Python script should print the installed TensorFlow version number. At the time of this writing, the TensorFlow version number is 2.12.0. You may have newer versions of installed TensorFlow on the Docker container while you’re reading this.

Once you’re done working with the TensorFlow Docker container, you can exit out of the terminal with the following command:

$ exit

Stopping the TensorFlow Docker Compose Project

To stop the TensorFlow Docker Compose project, run the following command while you’re in the ~/projects/docker-tensorflow project directory:

$ docker compose down

Conclusion

We showed you how to create a TensorFlow development environment with Docker and Docker Compose. We also showed you how to access the terminal of the TensorFlow Docker development environment and how to run the TensorFlow codes in the TensorFlow Docker development environment as well.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.