I’ve been trying to learn how to create and use docker and I thought that could be a nice idea to create a machine learning docker as a starting example to have something running.
But instead to use a existing docker image for machine learning I’ve try to create from scratch a full functional image that provide REST API machine learning services.
But in any case the full sourced of this is available in this git:
Before starting its worth mentioning that going through getting started with docker is a excellent starting point for understanding many of the docker topics and will give you the tools required.
First lets create a new folder for our docker image an the Dockerfile:
Now we add the initial lines to our docker file:
Lets build our docker, initial build will take a while:
And them run the docker in iterative way since there is nothing there yet.
In the iterative shell we could just try to list files and exit:
Lets add now python and pip to the docker image adding to the Dockerfile:
Now we could just rebuild and run:
Something like this will be output:
Now lets create a folder for our python app:
Lets create a small test python program in /python-app/test.py:
Modify the Dockerfile to call our python program:
Rebuild and run the docker will produce:
Sometimes we like to clean our docker system, if we do this we could see the containers:
To stop and remove our containers we could do:
Now we are going to change our python installation to be a machine learning installation, edit the Dockerfile and change the apt-install to be:
Now we could modify our test program /python-app/test.py:
And this will be the output:
Is time to do a simple sentiment analysis using TextBlob
Now we are going to create a folder for our configuration:
We will add this file for set python dependencies in /cfg/python-deps.txt
We are going to modify the Dockerfile to copy the config, and invoke pip to install dependencies, them we will download the corpus data for TextBlob:
So lets add some sentiment analysis to our test program:
This will be the output:
Now lets configure the flask web app.
First add to /cfg/python-deps.txt:
Modify Dockerfile to export a port and set and environment variable:
And finally lets sketch a simple test service in python-app/flask-web.py:
Now to run our docker in daemon and map the expose port to port 80:
Remember that the docker could be stop with:
We need to know the IP of our container, if we are using docker machine and we haven’t change the default VM we could use:
Them we could test using the url : http://IP/api/test
We should see this:
And we could see the log using :
This should output
Now lets create a basic REST service that return the sentiment from an string, we will add some basic error handling to our flask service, just edit /python-app/flask-web.py:
Them we could test our service:
About Juan Medina I'm just a normal geek that code all kind of stuff, from complex corporate applications to games.
Games, music, movies and traveling are my escape pods.