This project uses Terraform to run Flask API hosted on Docker.
Flask API will render the following templates Tindog Webapp
There are two ways to run the application by running Dockerfile and running the terraform.
Authors: Bijeck
Project uses a number of open source projects to work properly:
- Terraform - HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources.
- Python - Main programming language that lets project run effectively.
- Docker Engine - Docker Engine is an open source containerization technology for building and containerizing your applications.
- Docker Compose - Compose is a tool for defining and running multi-container Docker applications.
src
: contains source filessrc/templates
: contains html filesrc/static_files
: contains css and external files for HTML filesterraform
: contains Terraform Configurationrequirements.txt
: contains python requirements packages
Be Sure you have Virtulenv installed if not running below:
>> pip install virtualenv
After unzip the project, create a virtual environment with the following:
>> cd ETL_SuMP
>> virtualenv venv
Then active the virtual environment and install the packages:
# For Mac or Linux
>> source venv/bin/activate
# For windows
>> venv\Scripts\activate.bat
Install python packages to run project effectively:
>> pip install -r requirements.txt
-
Create a Docker image from Dockerfile and set tag of image to webapp:
>> docker build -t webapp .
Workflow in Dockerfile:
- Dockerfile will pull the Python image from Dockerhub.
- Create app folder as root folder.
- Copy requirements.txt to app folder and install dependencies.
- Copy src folder to app folder.
- Set the command will be executed when created container.
-
Check the image is created successfully:
>> docker images webapp
-
Create a Docker container from webapp image:
>> docker run -dp 5000:5000 --name webapp webapp:latest
Python webapp uses Flask to run and port of Flask is 5000.
So mapping the container port to localhost:5000 port.
-
Check the container is running:
>> docker ps -f name=webapp
-
Access localhost:5000 in browser to see content.
- To stop the container:
>> docker stop webapp
- To remove the container:
>> docker rm webapp
-
Initialize the terraform project with docker provider in main.tf:
>> cd terraform >> terraform init
All the configuation of docker provider and terraform will be downloaded.
-
Plan for terraform:
>> terraform plan
Terraform will plan how to run the project.
-
Run the project:
>> terraform apply
Terraform will ask for permission:
>> Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes
Terraform will use webapp image and add more configuation to run container.
-
Check the container is running:
>> docker ps -f name=webapp
Access localhost:5000 in browser to see content.
- To stop terrform project:
Terraform will ask for permission:
>> terraform destroy
This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state.>> Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes