Tensorflow is a free and end-to-end open-source machine learning platform created by the Google Brain team. It is a library used for numerical computation and large-scale machine learning. You can install it in a Python virtual environment or using Anaconda, as a docker container. It comes with a set of libraries and community resources that helps you to develop machine learning programs and applications.
In this tutorial, we will show you how to install Tensorflow with Docker on Ubuntu 20.04.
Step 1 – Install Docker CE
First, install all required dependencies with the following command:
apt-get install git apt-transport-https ca-certificates curl software-properties-common -y
Next, add the Docker GPG key and repository with the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Once the repository has been added, install the Docker and Docker compose with the following command:
apt-get install docker-ce -y
Once the installation is completed, verify the Docker version with the following command:
docker --version
You should see the following output:
Docker version 20.10.5, build 55c4c88
Step 2 – Launch Tensorflow Container
Docker provides a Tensorflow container for building machine learning applications.
First, download the Tensorflow container using the following command:
docker pull tensorflow/tensorflow:latest
You should get the following output:
latest: Pulling from tensorflow/tensorflow d519e2592276: Pull complete d22d2dfcfa9c: Pull complete b3afe92c540b: Pull complete c12ceea561ed: Pull complete 1f0ffb4d2509: Pull complete 961442e8e0e4: Pull complete 0b2b6f75fc95: Pull complete e266fced02fa: Pull complete c29b15b084af: Pull complete 5d43e8c7056a: Pull complete Digest: sha256:0118f3db64ddadf5d16f4b8d85ffb14228e194c0211c690562fb5dfd16fc86c0 Status: Downloaded newer image for tensorflow/tensorflow:latest docker.io/tensorflow/tensorflow:latest
Next, create a Tensorflow container from the downloaded image with the following command:
docker run -it -p 8888:8888 tensorflow/tensorflow:latest
You will be redirected inside the Tensorflow container:
________ _______________ ___ __/__________________________________ ____/__ /________ __ __ / _ _ \_ __ \_ ___/ __ \_ ___/_ /_ __ /_ __ \_ | /| / / _ / / __/ / / /(__ )/ /_/ / / _ __/ _ / / /_/ /_ |/ |/ / /_/ \___//_/ /_//____/ \____//_/ /_/ /_/ \____/____/|__/ WARNING: You are running this container as root, which can cause new files in mounted volumes to be created as the root user on your host machine. To avoid this, run the container by specifying your user's userid: $ docker run -u $(id -u):$(id -g) args... root@f8968eb6e582:/#
You can now write and run any application inside the container. You can exit from the container anytime using the following command:
exit
Step 3 – Launch Tensorflow Jupyter Container
Docker also provides a Tensorflow container with Jupyter notebook that allows you to write and run code through the Jupyter notebook dashboard.
You can download and launch the Tensorflow Jupyter container with the following command:
docker run -it -p 8088:8088 tensorflow/tensorflow:latest-jupyter
You should get the following output:
latest-jupyter: Pulling from tensorflow/tensorflow d519e2592276: Already exists d22d2dfcfa9c: Already exists b3afe92c540b: Already exists c12ceea561ed: Already exists 1f0ffb4d2509: Already exists 961442e8e0e4: Already exists 0b2b6f75fc95: Already exists e266fced02fa: Already exists c29b15b084af: Already exists 5d43e8c7056a: Already exists c09e41c80475: Pull complete 4928d4fd53b4: Pull complete 4f8c501e8020: Pull complete 9a927eacde80: Pull complete cca85cdd242a: Pull complete c491a3ee7337: Pull complete 70252b385326: Pull complete c385e49aff55: Pull complete 32a1800a0df4: Pull complete 56ac089c301a: Pull complete 32b4fcc94634: Pull complete 62fbf7c8b492: Pull complete a184bae0471c: Pull complete 3138b3afe589: Pull complete 322ff03a149a: Pull complete Digest: sha256:15ca1ea5083f1f9fcc2b006df4f1d51c3d0b6f2616fa14c12ff6dc7c4bc83588 Status: Downloaded newer image for tensorflow/tensorflow:latest-jupyter [I 03:08:37.757 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret jupyter_http_over_ws extension initialized. Listening on /http_over_websocket [I 03:08:38.106 NotebookApp] Serving notebooks from local directory: /tf [I 03:08:38.107 NotebookApp] Jupyter Notebook 6.2.0 is running at: [I 03:08:38.107 NotebookApp] http://40959756271a:8088/?token=2cbdd5bc2250270506f38954ed7f891a146cc71e50bc46fb [I 03:08:38.108 NotebookApp] or http://127.0.0.1:8088/?token=2cbdd5bc2250270506f38954ed7f891a146cc71e50bc46fb [I 03:08:38.108 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 03:08:38.114 NotebookApp] To access the notebook, open this file in a browser: file:///root/.local/share/jupyter/runtime/nbserver-1-open.html Or copy and paste one of these URLs: http://40959756271a:8088/?token=2cbdd5bc2250270506f38954ed7f891a146cc71e50bc46fb or http://127.0.0.1:8088/?token=
You can now access the Jupyter notebook using the URL http://your-server-ip:8088/?token=2cbdd5bc2250270506f38954ed7f891a146cc71e50bc46fb. You should see the following screen:
Conclusion
Congratulations! You have successfully installed Tensorflow with Docker on Ubuntu 20.04. You can now easily write and run a machine learning program with Tensorflow. Try it today on an Atlantic.Net VPS!