Docker

Basic Flask API with Docker

1. Create Flask File

./apy.py

from flask import Flask

app = Flask(__name__)

@app.route('/api/test')
def get_test():
    return 'test'

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

2. Create pip requirements.txt File

./requirements.txt

Flask==0.12

3. Create Dockerfile

./Dockerfile

FROM python:2
#The python image uses /usr/src/app as the default run directory:
WORKDIR /usr/src/app
#Copy from the local current dir to the image workdir:
COPY . .
#Install any dependencies listed in our ./requirements.txt:
RUN pip install --no-cache-dir -r requirements.txt
#Run api.py on container startup:
CMD [ "python", "./api.py" ]

4. Create Image

sudo docker build -t my_flask_api_img .

5. Run Container

sudo docker run -dit --name my_flask_api_container -p 80:80 my_flask_api_img

6. Test

Test by going to http://localhost/api/test