Project 1 : Dockerizing a Flask App

Project 1 : Dockerizing a Flask App

Introduction

In this blog we will see how to dockerize a flask app using dockerfile and run the project using docker-compose.

Task 1: Setup an EC2 machine.

  1. Log in to the AWS Management Console.

  2. Navigate to the EC2 Dashboard and click on the “Launch Instance” button.

  3. Enter a name for your EC2 instance.

  4. Select AMI, instance type and choose a key pair or create a new key pair.

  5. Create or select an existing Security Group and make sure port 22 is open to ssh into the instance.

  6. Click on Launch Instance.

  7. Connect to your instance either by 3rd party software like putty or MobaXterm or we can also connect using AWS EC2 instance connect.

Task 2 : Install docker and git on EC2 instance.

  • Update the system.
sudo apt update && sudo apt upgrade
  • Install docker and give necessary permissions.
sudo apt install docker.io
sudo usermod -aG docker $USER
sudo chmod 666 /var/run/docker.sock
  • Check Docker version.
docker --version
  • Install git.
sudo apt-get install git
  • Install docker compose.
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Task 3 : Cloning git repo and building docker image.

  • Cloning git repo from github ( In git repo we have a docker file , we will build an image from the dockerfile ).
git clone https://github.com/Abhishek-Verma99/Dockerizing-a-Flask-App.git
cd Dockerizing-a-Flask-App/
  • Dockerfile
FROM python:3.6
WORKDIR /app
COPY . /app
EXPOSE 5000
RUN pip install -r requirements.txt
ENTRYPOINT ["python", "app.py"]
  • Run the dockerfile to create an image .
docker build -t flask-app .
docker images

Docker Compose - Docker Compose is a tool for defining and running multi-container applications.

we do not have multi containers but still we can use docker-compose to run our flask app.

Task 4 : Running our app using docker-compose and accessing it from the browser .

  • docker-compose file
version: "3"
services:
  web:
    image: flask-app
    ports:
      - "5000:5000"
  • Run the docker-compose file to run the container .
docker-compose up -d

  • Accessing our app from the browser. .

    We can access our app in browser with the help of ip address of EC2 machine and port on which container is running.
    Note - Make sure to open the same port in inbound rules of your security group which is attched to your EC2 instance.

Public IPv4 address:port
Ex- 3.110.29.142:5000

Task 4 : Push image to Docker Hub .

  • First login to your DockerHub account using Command i.e docker login and give your username & password.

  • Then use docker push <DockerHub_Username>/<Imagename> for pushing to the DockerHub.

      docker push abhishekverma14/flask-app
    

You can see the pushed image in your dockerhub account.


Thanks all. Good luck out there!

Follow for more such amazing content :)

Happy Learning 😊