Featured image of post How to Depoy Flask App on Docker With Pulumi

How to Depoy Flask App on Docker With Pulumi

Create flask app

Create project directory

1
mkdir  -p pulumi-docker/flask-first-steps/app

create Dockerfile

1
2
3
4
tee pulumi-docker/flask-first-steps/Dockerfile << "EOF"
FROM tiangolo/uwsgi-nginx-flask:python3.8
COPY ./app /app
EOF

Create main.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
tee pulumi-docker/flask-first-steps/app/main.py << "EOF"
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    # Only for debugging while developing
    app.run(host='0.0.0.0', debug=True, port=80)
EOF

Option: Run app in docker

1
2
3
4
cd 
docker build -t myflaskservice .
docker run -d --name myflaskservice -p 8080:80 myflaskservice
curl 127.0.0.1:8080

Create Pulumi project

Create pulumi project at the same folder level

1
2
3
4
mkdir  -p pulumi-docker/pulumi-first-steps/
cd pulumi-docker/pulumi-first-steps/
pulumi login --local
pulumi new python

change requirements.txt

1
2
3
4
5
tee requirements.txt << "EOF" 
pulumi>=2.0.0,<3.0.0
pulumi-docker
protobuf==3.20.*
EOF

Active venv and install python modules

1
2
source venv/bin/activate
pip3 install -r requirements.txt

Modify __main__.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
tee __main__.py << "EOF"
"""A Python Pulumi program"""

import pulumi
import pulumi_docker as docker


myapp_build = docker.DockerBuild(context=f'../flask-first-steps')
myapp_image = docker.Image("myapp_image", image_name='myapp', build=myapp_build, skip_push=True)

container_ports = docker.ContainerPortArgs(internal=80, external=8080)
myapp_container = docker.Container("myapp_container", image="myapp", ports=[ container_ports])
EOF

container get started

1
2
export PULUMI_CONFIG_PASSPHRASE=""
pulumi up --yes

Check docker

1
2
docker ps
curl localhost:8080

Clean up

1
2
pulumi destroy --yes
pulumi stack rm dev --yes

Reference

Built with Hugo
Theme Stack designed by Jimmy