Go to file
Greg Gauthier 2fd3a5c2b0 add readme
2021-02-25 14:05:44 +00:00
service run tests as part of the build; simplify dockerfile; prep for bitbucket; 2021-02-25 13:41:02 +00:00
test run tests as part of the build; simplify dockerfile; prep for bitbucket; 2021-02-25 13:41:02 +00:00
.gitignore initial commit 2021-02-25 10:35:10 +00:00
Dockerfile run tests as part of the build; simplify dockerfile; prep for bitbucket; 2021-02-25 13:41:02 +00:00
Pipfile initial commit 2021-02-25 10:35:10 +00:00
README.md add readme 2021-02-25 14:05:44 +00:00

python-service-1

Running a python REST application in a docker container

The idea behind this is to simply provide a barebones model for how to run a python based REST "service" in a docker container.

You can replace this "service" application with any python application, really. I've also included a small suite of pytests, to demonstrate how you could use the test suite to break your build if you mess something up.

All of this could be instrumented in a pipeline, which I'll probably do on my bitbucket for demo purposes.

To build the container:

> docker build -t python-service-1 . 

You should get some output that looks like this:

> docker build -t python-service-1 .                                                                             (0m)|*[master]  
[+] Building 1.5s (13/13) FINISHED                                                                                                                           
 => [internal] load build definition from Dockerfile                                                                                                    0.0s
 => => transferring dockerfile: 37B                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/python:3.9                                                                                           1.4s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                           0.0s
 => [internal] load build context                                                                                                                       0.0s
 => => transferring context: 210B                                                                                                                       0.0s
 => [1/8] FROM docker.io/library/python:3.9@sha256:e2cd43d291bbd21bed01bcceb5c0a8d8c50a9cef319a7b5c5ff6f85232e82021                                     0.0s
 => CACHED [2/8] COPY service/__init__.py /service/                                                                                                     0.0s
 => CACHED [3/8] COPY service/requirements.txt /service/                                                                                                0.0s
 => CACHED [4/8] COPY service/simple.py /service/                                                                                                       0.0s
 => CACHED [5/8] COPY test/test_simple.py /test/                                                                                                        0.0s
 => CACHED [6/8] RUN pip install -r /service/requirements.txt                                                                                           0.0s
 => CACHED [7/8] RUN export PYTHONPATH=$PYTHONPATH:./;pytest -v /test/test_*.py -r A                                                                    0.0s
 => exporting to image                                                                                                                                  0.0s
 => => exporting layers                                                                                                                                 0.0s
 => => writing image sha256:b4f9200147f5c908367bd60826f4e716a7003bec4161b4481492c5d6278b1a56                                                            0.0s
 => => naming to docker.io/library/python-service-1                                                                                                     0.0s

If the pytest step fails, the build will fail.

To run the container:

> docker run -d -p 5000:5000 python-service-1 
ee5f7e9b5384777a9b52c3bf829b88e0bac1a3eb4e612bdd721d480724e37eb5

> docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS         PORTS                    NAMES
ee5f7e9b5384   python-service-1   "python /service/sim…"   10 seconds ago   Up 9 seconds   0.0.0.0:5000->5000/tcp   amazing_darwin

This should put the service on port 5000 on your localhost. You can hit it with curl commands for confirmation:

> curl -X POST -H "Content-Type: application/json" -d '{"name":"George"}' http://localhost:5000/hashname 
{"hash": 7024223046703969137, "name": "George"}% 

> curl http://localhost:5000/version 
{"application": "Simple Api", "version": 0.1}% 

One possible use for this, might be to teach API testing. It could also be coupled with a database or a web server, for additional testing demonstrations, for example, with selenium.