7. Commandline Tool

A lot of uses for Docker containers is to containerize enterprise applications. However, commandline tools may also be built using containers. Imagine that we have learned data science models that may predict

  • credit card fraud,

  • when an employee will leave a company or

  • if there is a network security threat.

How could we ship the model? In this example here, we mock a data science model that predicts how much a person is expected to earn per year based on the years of education they have after high-school. Let’s see how shipping data science models may be done. NOTE This model is fictitious.

Why would we want to containerize the models?

  • Ease of use: we may invoke the model easily through the Docker commandline.

  • Dependencies: we may avoid setting up complicated environments and dependencies required by our predictive models.

  • Portability: we may ship our model around (and sell them?).

7.1. Prediction model

The prediction program income-model.py is a Python application hosting our predictive model. It computes the expected yearly income earning as follows.

\[y = 25000 + 20000 \times e\]

If a person has no education after high-school, they are expected to make $25,000. The per unit increase in earning is $20,000 (per year after high-school).

 1import sys
 2import argparse
 3
 4def parse_args(args):
 5    parser = argparse.ArgumentParser('Income Predictor', 
 6        epilog='One-Off Coder https://www.oneoffcoder.com')
 7    
 8    parser.add_argument('-e', '--education', 
 9        help='Years of education', 
10        required=False, 
11        default=0, 
12        type=int)
13
14    parser.add_argument('-v', '--version', action='version', version='%(prog)s v0.0.1')
15
16    return parser.parse_args(args)
17
18def predict(e):
19    y = 25000.0 + 20000.0 * e
20    return y
21
22if __name__ == '__main__':
23    args = parse_args(sys.argv[1:])
24    
25    e = args.education
26    e = 0 if e < 0 else e
27    e = 20 if e > 20 else e
28
29    y = predict(e)
30
31    print(f'Esimated Income with {e} years past high school is is ${y:,.2f}')

7.2. Docker container

The Dockerfile below has 2 instructions ENTRYPOINT and CMD. Together, these instructions allow users to pass in the required arguments to the prediction model (which is years of education after high-school). In the Dockerfile below, ENTRYPOINT points to the executable program, and the CMD serves to pass in the arguments to the program.

1FROM python:3
2
3WORKDIR /program
4COPY ./income-model.py .
5
6ENTRYPOINT [ "python", "./income-model.py" ]
7CMD [ "--version" ]

Build the container as follows.

1docker build --no-cache -t income-model:local .

You may then run the container. Note the flag -e is required by the Python program. We could also substitute --education for -e as well. The flags after the container name and tag will be passed to the Python program and override what was originally in the CMD instruction (in the Dockerfile).

1docker run -it income-model:local -e 10

7.3. Downloads