Introduction
The goal of this exercise was to deploy a Flask AI application on an ec2 instance. This documentation covers a step by step method to achieve this using two methods- Dockerized & Without Docker
Deploying without Docker
Testing the application locally
As a rule of thumb, before deploying an application to a cloud environment, it is important to always attempt to get it to run locally to ensure there are little or no bugs.
Settting up Cloud environment
Create a GitHub repository to store the code if this hasn't been done already
Navigate to the AWS console, spin up an ec2 instance with with reasonable processing power- t2.medium should suffice
Make sure that when setting up the Security Group, an ingress rule for port 5000 is added.
Connect to the instance using a preferred method. Inside of the instance, run commands
git -v
to confirm the git command line tool is installed. If not, run either of the below commands to install it
# For Red-Hat based systems
sudo dnf install git-all
# For Debian based systems
sudo apt install git-all
# For MacOs, install using homebrew preferably
brew install git
# Verify installation
git -v
Clone repository and install dependencies
With the git cli tool installed, the repo can now be conveniently cloned
git clone <github-repo-url>
Install the Python & Pip
# Ensure Python is installed
python3 --version
# If not installed, run commands to install
# Debian based
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip
sudo apt install python3-dev python3-venv build-essential
# Red-hat based
sudo dnf update
sudo dnf install python3
sudo dnf groupinstall "Development Tools"
sudo dnf install python3-pip
# Confirm installation
python3 --version
Install Project dependencies
# Navigate to the Project directory
cd <project-name>
# Ensure the directory has requirements.txt file
ls
# Create Python virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run the application
python3 app.py
Finally, navigate to the browser, put in the public IP of the instance and attach port 5000 to it ie. 22.222.22.111:5000
Your application is now running on ec2
Deploying with Docker
Prerequisites
In the code base, a Dockerfile
has been created. This file is used to create the Docker Image alongside the compose.yaml
file.
To create the docker image locally and start the container, run:
docker compose up --build
This command builds the image using Docker Compose
Verify the container is running successfully by attempting to access the application on 127.0.0.1:5000
. Once confirmed, stop the container.
Next we need to tag the image so it can be pushed to Docker Hub.
# First verify the image name
docker images
# Your output should be something like spamemail-flask-app
# Now tag the image
docker tag spamemail-flask-app <your-dockerhub-username>/spam-email:latest
# Eg
docker tag spamemail-flask-app fifss/spam-email:latest
# Login to dockerhub from your terminal and follow the instructions
docker login
# Finally, push your image to DockerHub
docker push docker push <dockerhub-username>/spam-email:latest
Pull Docker image on ec2 instance
Create an instance on AWS. Make sure to use at least the t2.medium because of Docker system requirements.
Also be sure to allow the port 5000 in the Security Group for the instance
Login to the instance and install Docker. Refer to Docker docs
After going through the installation processes, to enable Docker without root privileges, refer to this documentation
Now test your Docker installation
docker run hello-world
Now that Docker is installed, pull the Docker image from DockerHub
docker pull <docker-username>/spam-email:latest
# Now that the image has been pulled, run the image as a container
docker run --name spam-email -p 5000:5000 <docker-username>/spam-email:latest
# Eg
docker run --name spam-email -p 5000:5000 fifss/spam-email
Open the browser and paste in the public IP address with the port 5000
# Example
http://10.222.133.155:5000/
Congratulations! Now your container is running successfully
Top comments (0)