Dockerizing a Flask application

Shruti
featurepreneur
Published in
1 min readJan 20, 2021

Creating a Flask application and dockerizing it to get the application up and running with just one command.

Docker and Flask

Prerequisites:

  1. Install & setup Docker

1. Create a Flask Application

In the terminal, cd into the folder you want your project in and create a file called app.py. Add the following code in it.

from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def hello():
return jsonify({"Message": "Hola!"})
if __name__ == "__main__":
app.run(host='0.0.0.0', port = 5001, debug=True)

2. Create Dockerfile

Create a file called Dockerfile at the root of your project and this code to it.

FROM python:alpine3.7 
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5001
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

To create a docker image:

sudo docker build -t flask-docker .

To run your application:

sudo docker run --name flask-docker -p 5001:5001 flask-docker

You can find your application running in http://0.0.0.0:5001/.

This project lives in Github at https://github.com/shrued/flask-docker.

--

--

Shruti
featurepreneur

Tech builds shouldn't be intimidating – I break down my projects in detail, turning my learning experiences into your shortcuts.