Skip to main content

Command Palette

Search for a command to run...

Flask (Python based) Application deployment through Docker-Compose

Updated
2 min readView as Markdown

Note: Total this project credit goes to Shri Shubham Londhe, here iam just providing commands readily for easiness of copy & paste

Steps:

  1. Create Ubuntu 22.04 EC2 (t2.micro)

  2. Install docker and docker compose

  3. clone the required source code repo from github

  4. run the needed commands of docker compose to run multi-tier container system in docker

  5. check in browser with ec2publicIP:5000 for our Flask Application

#Dockerfile
#Note: the below dockerfile is already in github repo, here just for reference
FROM python:3.9-slim
WORKDIR /app
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 requirements.txt .
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
#here docker-compose.yml file also given for ref, already in github repo
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:
sudo apt-get update -y
sudo apt install git -y
sudo apt install docker.io -y
sudo usermod -aG docker ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock
docker ps #(just to check docker working or not)
sudo apt-get install docker-compose -y 
#git clone https://github.com/your-username/your-repo-name.git
git clone https://github.com/LondheShubham153/two-tier-flask-app.git
cd two-tier-flask-app/
ls -lrt
#Dockerfile, docker-compose.yml files preparation is devops responsibility
docker-compose up     #(to start containers and deployments)
docker ps
docker images
docker-compose down   #(to stop containers & deployment)

More from this blog