Run Tensorflow on Mac using Docker

Here are the steps to run Tensorflow on Mac using Docker

1) Install docker

2)Launch docker container with Tensorflow CPU binary image and launch a Jupyter notebook

docker run -it -v /MY_LOCAL_COMPUTER_DIR:/MAPPED_DIR_ON_DOCKER -p 8888:8888 -p 6006:6006 tensorflow/tensorflow

Note: -v is to map your specified local directory to a directory on Docker. In this way, you can make your specified local directory accessible in your Docker instance. In my example command below, I specified this local directory on my computer /Users/weishungchung/aiformankind to be made available on Docker instance as /aiformankind.

docker run -it -v /Users/weishungchung/aiformankind:/aiformankind -p 8888:8888 -p 6006:6006 tensorflow/tensorflow

Screen Shot 2018-05-24 at 7.12.10 PM

3) Copy the Jupyter notebook URL printed on the console (replace localhost with, this is docker-machine default ip)

4) Log into Jupyter using the given token

Screen Shot 2018-05-24 at 7.14.53 PM
5) Open the hello_tensorflow.ipynb notebook and run it

Screen Shot 2018-05-24 at 7.19.50 PM

6) Open and run the 3_mnist_from_scratch.ipynb

Screen Shot 2018-05-24 at 7.23.47 PM

7) Install Keras

Log into docker container

Open another terminal, we want to log into container to install Keras.

On the terminal, run the command below

eval "$(docker-machine env default)"

Find your container id by running docker ps
You can see your container id in the console printout. In this example, mine is 2b89e5ae9430

docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                            NAMES
2b89e5ae9430        tensorflow/tensorflow   "/ --a…"   41 minutes ago      Up 41 minutes>6006/tcp,>8888/tcp   dazzling_villani

Then log into container by running the following command
(In this example, 2b89e5ae9430 is the container id)

docker exec -it 2b89e5ae9430  bash

Screen Shot 2018-05-24 at 8.01.00 PM

8) Once you are in the container, you are ready to install Keras

pip install keras

Screen Shot 2018-05-24 at 8.08.53 PM

9) Verify Keras version by running the following python command

python -c 'import keras; print(keras.__version__)'

Screen Shot 2018-05-24 at 8.17.11 PM

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s