Flask API Docker


app.py


from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/",methods=['GET','POST'])
def home():
    return jsonify({"payload":"Hello world, I am Leo."})

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=1111) # must be 0000
    

requirements.txt


Flask==1.1.2
Flask-Bcrypt==0.7.1
Flask-Classful==0.14.2
Flask-Cors==3.0.10
Flask-Ext==0.1
Flask-Jsonpify==1.5.0
Flask-Login==0.5.0
Flask-Markdown==0.3
Flask-MySQL==1.5.1
Flask-SocketIO==4.3.1
Flask-SQLAlchemy==2.4.3
Flask-WTF==0.14.3
    

Dockerfile


FROM python:3.8

COPY requirements.txt ./requirements.txt
COPY app.py ./app.py

RUN pip install -r requirements.txt

CMD ["python","./app.py"]
    

Commands

  1. Start container,
  2. 
    docker build -t app_docker -f Dockerfile .
    docker run -d -p 2222:1111 app_docker # out:in
        
  3. Stop and remove containers and images,
  4. 
    # list image
    docker image list
    # list container
    docker ps -a
    # stop and remove
    docker stop `docker ps -a | grep app_docker | awk '{print $NF}'`
    docker rm `docker ps -a | grep app_docker | awk '{print $NF}'`
    ## or
    docker rm --force `docker ps -a | grep app_docker | awk '{print $NF}'`
    # remove image
    docker image rm app_docker
        

References