This repository contains the setup and deployment instructions for a 2-tier web application using Docker and MySQL. The application is designed to run locally and integrates a MySQL database with a Python-based web application.
Additionally, the repository incorporates a Continuous Integration and Continuous Deployment (CI/CD) workflow using GitHub Actions. This automation simplifies the build and deployment process, allowing the application and its MySQL database images to be built and pushed to Amazon Elastic Container Registry (ECR) automatically whenever changes are made to the main branch.
- MySQL Integration: Easily set up and configure a MySQL database using Docker.
- Dockerized Application: Build and run the web application as a Docker container, simplifying the development environment setup.
- Environment Variable Configuration: Flexible configuration using environment variables for database connectivity and application settings.
- Local Development: Step-by-step instructions to run the application and database locally for testing and development.
- Automated CI/CD: Utilize GitHub Actions to automate the building and deployment of Docker images to Amazon ECR, ensuring seamless updates and consistency in the deployment process.
- Install the required MySQL package:
sudo apt-get update -y sudo apt-get install mysql-client -y
- Build the Docker images for both the MySQL database and the web application:
- Building MySQL Docker image:
docker build -t my_db -f Dockerfile_mysql .
- Building application Docker image:
docker build -t my_app -f Dockerfile .
- Building MySQL Docker image:
- Run the MySQL container:
docker run -d -e MYSQL_ROOT_PASSWORD=pw my_db
- Get the IP of the database and export it as the
DBHOST
variable:Example when running DB as a Docker container and app running locally:docker inspect <container_id>
Example when running DB as a Docker container and app running in Docker:export DBHOST=127.0.0.1 export DBPORT=3307
export DBHOST=172.17.0.2 export DBPORT=3306
- Set up other environment variables:
export DBUSER=root export DATABASE=employees export DBPWD=pw export APP_COLOR=blue
- Run the application and ensure it is visible in the browser:
docker run -p 8080:8080 -e DBHOST=$DBHOST -e DBPORT=$DBPORT -e DBUSER=$DBUSER -e DBPWD=$DBPWD my_app
This repository uses GitHub Actions for continuous integration and deployment (CI/CD). The workflow is defined in the .github/workflows/deploy.yml
file and includes the following steps:
- Check out Code: Pulls the latest code from the repository.
- Login to Amazon ECR: Authenticates with Amazon ECR using AWS credentials stored in GitHub secrets.
- Install Dependencies: Updates
pip
and installs the required Python dependencies. - Build and Push Main Application Image: Builds the Docker image for the main application and pushes it to ECR.
- Build and Push MySQL Image: Builds the Docker image for MySQL and pushes it to ECR.
- Docker installed on your machine.
- Python and required libraries as specified in the
requirements.txt
file. - An AWS account with permissions to use Amazon ECR.
- Amazon ECR repositories created for both the application and MySQL images.
- GitHub repository secrets configured with your AWS credentials:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
Follow the instructions in this repository to set up the application locally, and make sure it is accessible via your web browser. For automated deployments to AWS, push changes to the main
branch to trigger the GitHub Actions workflow.