Docker a complete Hands-On experience using Docker-Compose

Docker a complete Hands-On experience using Docker-Compose

Covering from days 44-46 and implementing in this project

Introduction

In this project, we'll explore the process of Dockerizing a Python Flask Redis app using Docker-Compose. We started by setting up Folder and files by cloning our newly created GitHub repository onto it. After installing Docker and Docker-compose, then we proceeded to containerize the application using a Dockerfile.

"We will be using VSCode to write codes"

Setting up the environment

  • Open your Ubuntu on your VM

  • After logging into your account, create a Folder named Docker-project

  • cd into Docker-project and clone the newly create repo from your GitHub repository using git clone

      git clone https://github.com/yourusername/Python-docker-project
    
  • Now open this folder in vscode application

  • Let's create some files first app.py

      from flask import Flask
      from redis import Redis
    
      app = Flask(__name__)
      redis = Redis(host='redis', port=6379)
    
      @app.route("/")
      def index():
          redis.incr('hits')
          counter = str(redis.get('hits'),'utf-8')
          return "Welcome to LegionDev, We have total "+counter+" viewers"
    
      if __name__=="__main__":
          app.run(debug=True, host="0.0.0.0")
    
  • Now Create a new file named as Dockerfile (without any extension)

      FROM python:3.11.0a6-alpine3.15
    
      WORKDIR /app
    
      COPY requirements.txt /app
    
      RUN pip install -r requirements.txt --no-cache-dir
    
      COPY . /app
    
      CMD python app.py
    
  • Create requirements.txt file and add the required dependencies

      flask
      redis
    
  • Create docker-compose.yaml file

      services:
          redis:
              image: redislabs/redismod
              container_name: redis
              ports:
                  - '6379:6379'
          web:
                  build: .
                  container_name: web
                  ports:
                      - "5000:5000"
                  volumes:
                      - .:/app
                  depends_on:
                      - redis
    
  • After setting up the environment Install docker desktop and docker-compose

  • Install Docker desktop on Ubuntu(Debian):

    • Download .deb from here: Docker-Desktop.deb (Debian)

    • Now Install prerequisites in order to Install Docker Desktop on ubuntu system

        sudo apt install -y ca-certificates curl gnupg lsb-release
        sudo mkdir -p /etc/apt/keyrings
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
        echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
        sudo apt update -y
      
      • Run the below command once you have downloaded DEB package and installed above prerequisites
        sudo apt install ./docker-desktop-<version>-<arch>.deb
  • Now push the code into your remote repository which we created in the initial stage

  • First Install and setup your git on your system and run the following commands 1 by 1.

      # To update libraries
      sudo apt update
      # To install git
      sudo apt install git
      # To check the installed version
      git --version
    
  • Now set your global config profile

      git config --global user.email "youremail@gmail.com"
      git config --global user.name "YourUserName"
    
      git add .
      git commit -m "Added files to repo"
      git push -u origin main
    
  • Now open Docker Engine and run following commands

      docker-compose up
    

And that's it now your application is Successfully Dockerized

  • To check the running containers run below command:

      docker ps
    

Did you find this article valuable?

Support Jatin Chourasia by becoming a sponsor. Any amount is appreciated!