- 1. Introduction
- 2. Setup
- 3. Testing
- 4. Clean Up
- 5. References
- 6. Author
- 7. Contributing
- 8. Conclusion
This guide demonstrates a basic multi-container setup with networking. It showcases a simple web application served by an Nginx web server, interacting with a MySQL database. The web app, built with HTML, CSS, and JavaScript, utilizes the Nginx image as a base, while the database relies on MySQL.
The primary goal is to illustrate how to set up a multi-container application with networking using Terraform. Keep in mind that this example is for educational purposes and not intended for production use. Success is achieved when the two Docker containers effectively communicate.
- Docker
- Terraform
The project's architecture is shown below:
-
Clone the repository.
-
Go to the project's root directory.
-
Create a file named
terraform.tfvars
in the root directory with the required variables:project_name="" web_app_image="" db_image="" app_names="" mysql_config=""
Note:
app_names
is a list(string) variable, andmysql_config
is a map(string) variable. Providemysql_config
in this format:mysql_config = { MYSQL_ROOT_PASSWORD = "" MYSQL_DATABASE = "" MYSQL_USER = "" MYSQL_PASSWORD = "" }
The
app_names
variable should look like:app_names = ["web_app", "db"]
Note: Ensure you pull the database image from Docker Hub before running the Terraform configuration. The image used in this example is
mysql:latest
. -
Run these commands:
terraform init terraform validate terraform plan -var-file="terraform.tfvars" terraform apply
-
Access the web application at
http://localhost:8080
.
After applying the Terraform configuration, the web application should be accessible at http://localhost:8080. It should connect to the database successfully and display data.
To verify the connection between the containers, run the commands in the image below:
The image confirms successful communication between the web application and the database.
To clean up, run:
terraform destroy -auto-approve
Note: Ensure the containers are stopped before running the terraform destroy
command.
Contributions, issues, and feature requests are welcome!
To contribute, fork the repository, make changes, and push them back by creating a pull request. I will review and merge if satisfactory.
In conclusion, this project provides a simple example of setting up a multi-container application with networking.
Terraform is used to deploy the Docker containers locally. Success is achieved when the two Docker containers communicate effectively, this is achieved by placing both containers on the same network.