Nono.MA

TensorFlow with Docker in 5 minutes

NOVEMBER 9, 2022


How to use TensorFlow inside of a Docker container.

Chapters

Transcript

Hi everyone.

It's Nono here, and this is a really quick video on how you can run TensorFlow on your machine inside of Docker, even if TensorFlow cannot be installed with pip or with Python on your machine. Right?

What are Docker and TensorFlow?

For newcomers, people who don't know this, Docker is a program that lets us virtualize Linux machines and other types of machines in our Mac or Windows, and TensorFlow is a machine learning, a machine learning framework by Google, which if you're here, you probably know already, all right?

Quick reminder, uh, please like the video if you would like to see more content or you enjoy this video and subscribe and click on the bell if you're gonna get notified when I upload new videos.

All right.

Create a Docker container - Overview

We have three commands here.

The first one is docker run -it.

So Docker and Run basically just runs a new container using an image that you give it. Here the important part is the -i flag that is going to create an interactive session for us.

You have here python:3.9-slim, which is the image that we're gonna use on our container and we have bash, so we start an interactive session.

When we have the interactive session, we'll be able to run pip install tensorflow tensorflow-io.

This is TensorFlow and the TensorFlow IO package.

So I can then run the Python command that I have in the code.

Right?

Create a Docker container - Demo

All right, so let's actually take a look at how to do that.

I have a window here and I also have Docker. If I run here... docker run interactively, and I select the python:3.9-slim image, I'm going to say I want to start a bash um, session, and I'm gonna actually name this tf so we know what container we're talking about.

This gets started really quickly. I already have this Python image on my machine. In yours it might have to download it. And we can see here that indeed we have that new container that has a Python image called tf here.

We could stop it or we could do whatever. When we exit, we'll see.. Does this actually get stopped?

Reconnect to the container - docker exec

How do we get back to it? If we get it stopped, we click play, we now run here... docker exec -it, the name of the container, and then what command we want to run on it.

All right, so we're back on our container.

Install Python packages - TensorFlow and TensorFlow IO

Now I can check here that I have Python 3 and that I have pip. And we're gonna straightforward do pip install tensorflow and tensorflow-io.

This is gonna take some time and I'm gonna explain a few things here.

This is installing TensorFlow, not on your machine, but on a virtualized machine that is actually a Linux machine.

Indeed, you can actually see that it's installing tensorflow_cpu_aws.

That's a specific build of TensorFlow that is meant to run on cloud instances, probably on AWS, right? That's Amazon Web Services.

After all these packages have been resolved, pip is installing them on that container. Remember, this container is running in this Docker container, and if I delete or stop that container, TensorFlow is like it's not installed in your machine anymore.

If you don't delete the container after stopping it, you can connect back to it and continue using TensorFlow.

Check the TensorFlow version

All right. So it seems like that's done. So I'm gonna click here on Python.. Verify that the same Python version is present if I just do Python. So I'm gonna go into Python, I'm gonna import TensorFlow as tf.

We're gonna first print to see what TensorFlow version I have, and now we can test a few operations here. So for example, tf.constant is gonna return a constant and convert_to_tensor with a simple Python array is going to return a tensor shape (3,), right?

Call to action

I think this is it. So this was Nono Martinez Alonso with a really quick video on how to run TensorFlow on your machine.

Remember you can like this video if you wanna let me know that this was something useful for you and don't forget to subscribe and click on that bell if you're gonna get notified when I upload new videos.

Start and re-attach to an existing container

For those of you who are still there, I'm gonna show really one quick thing that clarifies everything.

I think if I exit again, a Python and the container, you can see here that that container is still running. I can stop it so we can stop the container. We can reattach to it and I'm back at it, right?

Uh, but what happens if I now try to use my Python version or my Mac and try to actually do that? So tensorflow as tf.

So TensorFlow is not there.

All you have to do is go back to your container, start it, and once that's started, you just run docker exec -it, the name of your container, and then you can directly prompt the python3 command to get a Python session.

Here we'll have tensorflow as tf, and you can see that that's being properly imported.

Outro

All right.

It was Nono Martinez Alonso here.

Thanks a lot for watching and I'll see you next time.

BlogVideoCodePythonMlTensorflowDocker