Basic Flask API with Docker

1. Create Flask File


from flask import Flask

app = Flask(__name__)

def get_test():
	return 'test'

if __name__ == '__main__': = '', port = 80, debug = True)

2. Create pip requirements.txt File



3. Create 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 on container startup:
CMD [ "python", "./" ]

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