This tutorial guides you through deploying a Flask application with PostgreSQL using Terraform and Docker. Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.
- Basic understanding of Python, Flask, and SQLAlchemy.
- Docker installed on your system.
- Terraform installed on your system.
app.py
: Flask application.Dockerfile
: Docker configuration for the Flask app.main.tf
: Terraform configuration for the deployment.
-
Open your terminal.
-
Navigate to your project directory.
-
Run the following command to initialize Terraform:
terraform init
This command initializes Terraform, installs the Docker provider, and prepares your project for further actions.
Terraform uses the terraform plan
command to create an execution plan. This command lets you preview the changes that Terraform will make to your infrastructure:
-
In your terminal, run:
terraform plan
-
Review the output to understand what resources Terraform will create or modify.
After planning, apply the configuration to create the infrastructure:
-
Execute the following command:
terraform apply
-
Confirm the action by typing
yes
when prompted.
Terraform will now create the Docker network, build the Flask app Docker image, and deploy the PostgreSQL container as specified in your main.tf
file.
- Once Terraform successfully applies the configuration, your Flask application will be running inside a Docker container.
- Access the application at
http://localhost:5000
or the IP address of your Docker host.
When you no longer need the infrastructure, you can destroy it:
-
Run:
terraform destroy
-
Confirm the action by typing
yes
when prompted.
This command will remove all the resources managed by Terraform, effectively stopping and removing the Docker containers.
Certainly! Let's break down the main.tf
file from your project, explaining each part in detail.
The main.tf
file is the core of your Terraform configuration, defining the infrastructure that Terraform will manage. Here's an explanation of its contents:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13"
}
}
}
provider "docker" {
# Configuration options
}
- Terraform Block: This section specifies the Docker provider and its version. Terraform uses providers to interact with different services like AWS, Azure, or, in your case, Docker.
- Provider "docker": This declares the use of the Docker provider. The configuration options within this block would be specific to Docker, like specifying a host if not using the default local setup.
resource "docker_network" "app_network" {
name = "app-network"
}
- Resource "docker_network": Defines a Docker network resource named
app-network
. This network will be used to connect your Docker containers.
resource "docker_image" "flask_app_image" {
name = "flask_app"
build {
context = "${path.module}/Dockerfile"
}
}
- Resource "docker_image": This resource builds a Docker image for your Flask app. The
name
is set toflask_app
, and thebuild
context points to yourDockerfile
, indicating where Docker should look to build the image.
resource "docker_container" "flask_app_container" {
name = "flask_app"
image = docker_image.flask_app_image.image_id
ports {
internal = 5000
external = 5000
}
networks_advanced {
name = docker_network.app_network.name
}
}
- Resource "docker_container" for Flask: Defines a Docker container for your Flask application. It references the image built in the previous step and maps the container's internal port 5000 to an external port 5000. The container is also attached to the previously defined network.
resource "docker_image" "postgres_image" {
name = "postgres:latest"
}
- Resource "docker_image" for PostgreSQL: Pulls the latest PostgreSQL image from Docker Hub.
resource "docker_container" "postgres_container" {
name = "postgres"
image = docker_image.postgres_image.image_id
env = [
"POSTGRES_DB=exampledb",
"POSTGRES_USER=exampleuser",
"POSTGRES_PASSWORD=examplepass"
]
networks_advanced {
name = docker_network.app_network.name
}
}
- Resource "docker_container" for PostgreSQL: Creates a Docker container for PostgreSQL. It uses the PostgreSQL image pulled earlier and sets environment variables for the database name, user, and password. This container is also part of the
app-network
.
This main.tf
file creates a Docker network, builds and runs a Docker container for the Flask application, and sets up a PostgreSQL database, all orchestrated by Terraform. The resources are interconnected, demonstrating how Terraform manages and provisions interdependent infrastructure components in a declarative way.
Congratulations! You've successfully used Terraform to deploy a Flask application with PostgreSQL using Docker. This tutorial covered the basics of Terraform's workflow including initialization, planning, applying, and destroying infrastructure.
- For more detailed information on Terraform, visit HashiCorp's Terraform Tutorials.
- To dive deeper into Terraform basics, check out An Introduction to Terraform for Beginners.
- For advanced Terraform concepts and techniques, refer to GravityDevOps' Guide on Terraform.