Code Monkey home page Code Monkey logo

devops-project-5's Introduction

DevOps-Project-5

Untitled design

Project Blog link :-

Project Overview :-

  • ๐Ÿš€ Building a Resilient Flask App with MySQL, Docker, and Docker-Compose๐ŸŒ
  • ๐ŸŒ Application Overview : Iโ€™ve developed a Flask-based web application that seamlessly interacts with a MySQL database. Users can submit messages through the user-friendly interface, and these messages are securely stored in the MySQL database, then promptly displayed on the frontend.
  • ๐Ÿณ Docker Containerization : For streamlined packaging and deployment, Iโ€™ve containerized this application with Docker. This ensures consistent behavior across different environments and simplifies the deployment process.

Project Steps :-

  • Create 1 ec2 instance : 2-Tier-App-Docker : AWS Linux-2, t2 micro

    image

  • Goto Security-> security group-> Edit inbound rules-> Add rule-> choose All Traffic

    image

1.1 Install and Configura on venv on Ubuntu 22.04 :-

python3 -m venv venv ## Creaing venv
source venv/bin/activate ## Activeting venv
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
pip install mysqlclient==2.1.1
pip install -r requirements.txt
export MYSQL_USER=db-user-name
export MYSQL_PASSWORD=db-user-password
export MYSQL_DB=db-name
python3.10 app.py ## running App

1.2 Install and Configure the Docker :-

ubuntu
sudo su
apt update -y
apt install docker.io -y
hostnamectl set-hostname docker
bash
docker ps
whoami
chown $USER /var/run/docker.sock



2. Clone the Github code :-

git clone https://github.com/rutikdevops/DevOps-Project-5.git
cd DevOps-Project-5



3. Create Docker File :-

vi Dockerfile

# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# install required packages for system
RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y gcc default-libmysqlclient-dev pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Copy the requirements file into the container
COPY requirements.txt .

# Install app dependencies
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

# Specify the command to run your application
CMD ["python", "app.py"]
docker build . -t flaskapp          #  Create a docker image from Dockerfile
docker images
docker network create twotier       #  Now, make sure that you have created a network using following command
  • Attach both the containers in the same network, so that they can communicate with each other
docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin --name=mysql mysql:5.7

docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDb --name=flaskapp flaskapp:latest

docker ps
docker network ls
docker network inspect twotier



3. Create the messages table in your MySQL database:

docker ps
docker exec -it <paste here mysql container-id> bash
mysql -u root -p
admin                       # enter password = admin
show databases;
use myDb;
# Paste this code in myDb
CREATE TABLE messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    message TEXT
);
  • Now run the app in browser :-

    image

  • To show the data entered in App :-

docker exec -it <paste here mysql container-id> bash
mysql -u root -p
admin                       # enter password = admin
show databases;
use myDb;
select * from messages;      # To show the data entered in App



4. Push Docker Image in DockerHub :-

docker login
# Enter username & password
docker tag flaskapp:latest <github_username>/flaskapp:latest
docker images
docker push <github_username>/flaskapp:latest

image

5. Use docker-compose

  • Run App using one single command instead of running both the containers one by one :-
apt install docker-compose -y
vi docker-compose.yml

///
version: '3'
services:
  
  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql   # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:
  mysql-data:
///

docker ps
docker kill (paste here container-id)
docker rm (paste here container-id)
docker-compose up -d
# Now your docker-compose app is running
  • App is running in Docker-compose.

    image

Project Reference :-

devops-project-5's People

Contributors

rutikdevops avatar t-chyngyz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.