Python code as service in Docker

File Name: calc.py

#This runs as service.

from flask import Flask
app = Flask(__name__)

@app.route("/sum/<int:a>/<int:b>/")
def sum(a,b):
    sum = int(a) + int(b)
    #print "sum is", sum
    return str(sum)

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')
    

————
File Name: requirements.txt

flask==0.12.1
————

File Name: Dockerfile

# Use an official Python runtime as a base image. Get this version from local system with >python –version
FROM python:2.7.10

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt. These are packages used in python script.
RUN pip install -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME calc

# Run calc.py when the container launches
ENTRYPOINT [“python”, “calc.py”]

————
>docker build -t calc .

>docker run -p 5000:5000 -it calc

In browser
http://localhost:5000/sum/1/2/
3

Note: Remove debug=true and run in background mode in real time.
————–
1. This makes easy to integrate with docker.
2. Load balancing need to be taken care separately.
3. Scaling is easy based on demand
4. Easy to deploy/redeploy patches
All advantages of micro services.
-o-

Docker – Management, Clients

How to manage multiple instances of Docker in production?
http://rancher.com/
https://kubernetes.io/

How to connect to Docker?

Option 1: Thick clients, We can start/stop/build containers and lot more.
https://docs.docker.com/engine/api/sdks/#other-languages

Spotify Client: https://github.com/spotify/docker-client (Supports latest Docker version)
Docker Java: https://github.com/docker-java/docker-java

This is recommended when we need to do heavy lifting work with containers.

Option 2: Thin Client:

HTTPClient is good enough, when Docker exposes services.
This is more appropriate way to design docker container and communicate with docker container.

Option 3: Using Java Shell and hitting docker commands.
This is not recommended when loading/unloading process is costly.
Also assuming that docker is available in shell.

Option 4: Using Docker REST urls
https://docs.docker.com/engine/api/v1.26/#operation/ContainerList

This gives more control on Docker container. Useful to deal with low level.
Docker is keep on changing their interfaces and causing trouble to developers/production systems.

-o-

Build Docker Container

Step 1: Download and install docker
https://www.docker.com/community-edition
110MB File

Step 2: Install image

$docker version

Step 3: Build docker image
https://docs.docker.com/get-started/part2/#build-the-app

https://en.wikipedia.org/wiki/Flask_(web_framework)

Create following three files in empty folder.
——————
File Name: cacl.py

import sys
a = sys.argv[1]
b = sys.argv[2]
summ = int(a) + int(b)
print "sum is", summ

——————
File Name: Dockerfile

# Use an official Python runtime as a base image. Get this version from local system with >python –version
FROM python:2.7.10

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt. These are packages used in python script.
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME calc

# Run calc.py when the container launches
ENTRYPOINT [“python”, “calc.py”]

——————
File Name: requirements.txt (empty)

——————

>docker build -t calc .

>docker images
We should be able to see calc image

>docker run -p  calc 2 3
sum is 5

-o-

Problems:
This is first simple docker image with Python code.
Every time we run, it creates image from cache and executes and dies.
Leaving many unused images, accumulating space.
>docker ps -a

We can see all past images, which are not existed.