Iām taking classes in Machine Learning for Winter 2021, at UC Irvine. Weāre required to do quite a lot of assignments and projects using Python and submit them. With my love for docker slowly increasing over the past few years, I decided to see if I could setup my local environment using docker. And based on the title of the blog post, you know that I found a way. š
Weāre going to setup a Jupyter notebook based environment.
Itās as simple as running this in your terminal. More detailed information is provided below
docker run -p 8888:8888 --name jupyter-notebook jupyter/tensorflow-notebook
jupyter/tensorflow-notebook
image as mentioned here. You could choose any other image as per your needs as well.docker run
-p 8888:8888
--name jupyter-notebook
jupyter/tensorflow-notebook
Ā in a terminal to
jupyter/tensorflow-notebook
image. Warning: Around 3.5GB.For reference, once you run the above command, you should see something like this in the terminal
The docker image comes with Python 3 pre-installed, along with a bunch of ML libs like numpy, matplotlib, TensorFlow, Keras etc. You can create a Python 3 notebook and run the following code to verify the TensorFlow version
import tensorflow as tf
print(tf.__version__)
I will be using jupyter-notebook
everywhere below, as Iāve named my container as that. You can substitute it with whatever name you chose during the run
command. Also remember that you donāt necessarily need to name your container. You could also just reference it by the unique id it is assigned. To see your container, run docker ps -a
.
Use Ctrl + C
in the terminal that youāve run the run
command to kill the container. You can also run docker stop jupyter-notebook
in another terminal. But I donāt know why you would want to do that. š¤·
Remember that the state is saved and when you restart the container, you can just continue. Run docker start -a jupyter-notebook
to start the container and attach the output of the container to your terminal.
There are two ways, and either seems to do the job
docker exec
-it jupyter-notebook
/bin/bash
in another terminal on your machine.Itās highly likely that you have some data on your machine and youād want to create a notebook there. Or would just like to use existing notebooks on a folder on your machine. There is an option -v
as part of the run
command, that you can use to āmountā a folder onto your docker container. I might be messing up terminologies, but it basically lets you share a folder with your docker container. Imagine you wanted to share a folder /usr/code/ml
with your container. You could run the same run
command as before, using -v
/usr/code/ml:/home/jovyan/dev
.
This gives access to the usr/code/ml
folder on your machine to the docker container and is accessible at /home/jovyan/dev
. This path in the docker container is specific to this image, as the jupyter notebook
command is run in this folder when you create the container.
An astute observer wouldāve noticed that I said use -v
when you run
. Yes, thatās right. You canāt share a folder with a container once it has been created already. So, you canāt use the -v
option with docker start
, for instance.
I hope this was a nice real world example of using docker to setup an ML environment. I love docker because itās fast, intuitive, and also pretty much an industry standard for containerization. Though Iāve used docker for micro-services in the past, and also seen Kubernetes in production, I really like using docker for my personal use for stuff like this. I have found some other nice personal use cases that I hope to write about in the future!