How to containerize flask app in GCP

Container Registry in Google Cloud Platform used for storing, managing and securing Docker container images.

In this tutorial we will see how to containerize Flask App with Container Registry. Lets start by following below steps.

Login to Google Cloud Console

Activate Cloud Shell to open Cloud Shell.

Create new directory with name mp_app and navigate to mp_app


mkdir my_app

cd my_app

In mp_app create a file with name Dockerfile


nano Dockerfile

Write below code in Dockerfile


FROM python:3
WORKDIR /app
ADD . /app


RUN pip install -r requirements.txt


EXPOSE 80

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

In mp_app create a file with name requirements.txt


nano requirements.txt

Write below code in requirements.txt


Flask

In mp_app create a file with name app.py


nano app.py

Write below code in app.py


from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
    msg = "<h1>Hello Flask</h1>"
    return msg

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

Build Docker Image

For building Docker Image run below command from mp_app directory


docker build -t my_app-image .

Tag Docker Image

For tagging Docker Image run below command, replace [project-id] with your GCP PROJECT-ID.


docker tag my_app-image gcr.io/[project-id]/my_app-image:version1.0

Push Docker Image to Container Registry

For pushing Docker Image to Container Registry run below command, replace [project-id] with your GCP PROJECT-ID.


docker push gcr.io/[project-id]/my_app-image:version1.0

Pull Docker Image from Container Registry

For pulling Docker Image from Container Registry run below command, replace [project-id] with your GCP PROJECT-ID.


docker pull gcr.io/[project-id]/my_app-image:version1.0

Note: If you are facing any issue issue with this set up, post the error in comment section, we will try to help you on that.

Categories: GCP