Skip to main content

Deploying salary prediction ML model inside a Docker container hosted on an EC2 instance:


Deploying your salary prediction ML model inside a Docker container hosted on an EC2 instance


A step-by-step guide to deploying your salary prediction ML model inside a Docker container hosted on an EC2 instance:

Step 1: Prepare the ML Model

  1. Train your model: Make sure your salary prediction model is trained and saved as a serialized file (e.g., model.pkl).
  2. Create a Flask API: If you haven't already, create a Flask API to serve the model predictions.
    from flask import Flask, request, jsonify
    import pickle app = Flask(__name__) # Load the model model = pickle.load(open('model.pkl', 'rb')) @app.route('/predict', methods=['POST']) def predict(): data = request.get_json() prediction = model.predict([data['features']]) return jsonify({'prediction': prediction.tolist()}) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
  3. Test the API locally: Run the Flask application locally to ensure it works as expected.

Step 2: Create a Dockerfile

  1. Create a Dockerfile in the same directory as your Flask app. Here's an example:
    # Use an official Python runtime as a parent image
    FROM python:3.8-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the world outside this container EXPOSE 5000 # Run app.py when the container launches CMD ["python", "app.py"]
  2. Create a requirements.txt file to list the dependencies
  3. Flask
    scikit-learn

Step 3: Build and Test the Docker Image Locally

  1. Build the Docker image:
    docker build -t salary-prediction-app .
  2. Run the Docker container locally:
    docker run -p 5000:5000 salary-prediction-app
  3. Test the API: Use Postman or curl to test your API endpoint (http://localhost:5000/predict).

Step 4: Set Up an EC2 Instance

  1. Launch an EC2 instance: Go to the AWS Management Console, launch an EC2 instance, and choose an appropriate AMI (e.g., Amazon Linux 2).
  2. Connect to the EC2 instance:
    ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-dns
  3. Install Docker on the EC2 instance:
    sudo yum update -y
    sudo amazon-linux-extras install docker sudo service docker start sudo usermod -a -G docker ec2-user

Step 5: Deploy the Docker Container on EC2

  1. Copy your Docker image to the EC2 instance:

    • You can use docker save and docker load commands to transfer the Docker image, or you can push the image to a Docker registry (e.g., Docker Hub) and pull it on the EC2 instance.
    docker save salary-prediction-app | gzip > salary-prediction-app.tar.gz
    scp -i /path/to/your-key.pem salary-prediction-app.tar.gz ec2-user@your-ec2-public-dns:/home/ec2-user/ ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-dns gunzip -c salary-prediction-app.tar.gz | docker load
  2. Run the Docker container on EC2:

    docker run -d -p 80:5000 salary-prediction-app

Step 6: Access Your Application

  • Access your app: The app should now be running on your EC2 instance. You can access it using the public DNS of your EC2 instance:
    http://your-ec2-public-dns/

Step 7: Secure Your Application

  • Security groups: Make sure your EC2 instance security group allows inbound traffic on port 80 (HTTP).
  • Optional: Set up a domain name and SSL for better security and accessibility

Your Application

  • Security groups: Make sure your EC2 instance security group allows inbound traffic on port 80 (HTTP).
  • Optional: Set up a domain name and SSL for better security and accessibility

Comments

Popular posts from this blog

Unlocking the Power of CGI-BIN: A Dive into Common Gateway Interface for Dynamic Web Content

 CGI-BIN What is CGI-BIN? The Common Gateway Interface (CGI) is a standard protocol for enabling web servers to execute programs that generate web content dynamically. CGI scripts are commonly written in languages such as Perl, Python, and PHP, and they allow web servers to respond to user input and generate customized web pages on the fly. The CGI BIN directory is a crucial component of this process, serving as the location where these scripts are stored and executed. The CGI BIN directory is typically found within the root directory of a web server, and it is often named "cgi-bin" or "CGI-BIN". This directory is designated for storing executable scripts and programs that will be run by the server in response to requests from web clients. When a user interacts with a web page that requires dynamic content, the server will locate the appropriate CGI script in the CGI BIN directory and execute it to generate the necessary output. One of the key advantages of using

Mastering Docker: A Comprehensive Guide to Containerization Excellence

  DOCKER Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called   containers   that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run. Running Docker on AWS provides developers and admins a highly reliable, low-cost way to build, ship, and run distributed applications at any scale. Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and self-sufficient units that can run applications and their dependencies isolated from the underlying system. Docker provides a set of tools and a platform to simplify the process of creating, deploying, and managing containerized applications. Key components of Docker include: Docker Engine: The core of Docker, responsible for building

What is Fuzzy Logic?

 Title: Demystifying Fuzzy Logic: A Primer for Engineering Students Introduction In the world of engineering, precise calculations and binary decisions often reign supreme. However, there are real-world scenarios where the classical "yes" or "no" approach falls short of capturing the nuances of human thought and the complexity of certain systems. This is where fuzzy logic comes into play. Fuzzy logic is a powerful tool that allows engineers to handle uncertainty and vagueness in a more human-like way. In this article, we'll explore the basics of fuzzy logic, its applications, and how it can benefit engineering students. Understanding Fuzzy Logic Fuzzy logic, developed by Lotfi Zadeh in the 1960s, is a mathematical framework that deals with reasoning and decision-making in the presence of uncertainty and imprecision. Unlike classical binary logic, which relies on "true" or "false" values, fuzzy logic works with degrees of truth, allowing for a