Docker-Projects(Step-by-step Implementation)

Docker-Projects(Step-by-step Implementation)

1. Deploying a Flask application using the Docker-Compose concept.

  1. First, let's update our System and install docker.

    sudo apt-get update && sudo apt-get install docker.io

  2. Now you can validate that docker is running as well as its version

    docker --version

    systemctl status docker

  3. Now let's install docker-compose

    sudo apt install docker-compose

  4. Now let's clone our GitHub repo.

    git clone https://github.com/subho45631/flask-app-ecs.git

  5. Now we can see our flask directory and its contents, now we need to create a docker-compose file

  6. vim docker-compose.yaml

  7. Start the container application in detached mode.

    docker-compose up -d

  8. To check the container running.

    docker ps

Please check in the rightmost column which show's up flask-app-ecs_flask-app_1

2. Project 2 -(Dockerizing a web application using a Dockerfile)

  1. First Clone your GitHub repo:

    git clone https://github.com/subho45631/flask-calculator.git

  2. Create a Dockerfile:

    vim Dockerfile

    ubuntu@ip-172-31-92-146:~/projects/flask-calculator$ cat Dockerfile

    FROM python:3.10

    WORKDIR /opt/app

    COPY . /opt/app

    RUN pip install -r requirements.txt

    EXPOSE 3000

    CMD ["python3","app.py","0.0.0.0.3000"]

  3. docker build . -t flask-cal-app

  4. docker images -------to check image build

  5. No run it as a container

    docker run -d -p 3000:3000 flask-cal-app:latest

  6. Now allow port no:3000 in your ec2 instance by editing the inbound security rule

  7. Now copy your public IP address and paste it with your port no.

    <ip address>:3000

3. Project 3-Deploying web app using Docker Swarm. (Production Ready).

  1. Follow the steps:

    First of all, go to the AWS portal and create three new instances. Named:

    · Swarm-manager

    · Swarm-worker1

    · Swarm-worker2

  2. Install docker and add a docker to the user group in master as well as worker nodes.

    sudo apt-get update && sudo apt-get install docker.io

    sudo usermod -aG docker $USER

    sudo reboot

  3. Now, Open the “Swarm Manager” node and run the command,

    sudo docker swarm init

  4. In all three instances, inside the inbound rules, allow

    · Custom TCP -- 2377 -- Anywhere IPv4-------which you will get from swarm-master as soon as you initialize it.

    · Custom TCP -- 8001 -- Anywhere IPv4------ app port number which you need to run the application successfully

  5. After initializing the swarm on the “swarm-manager” node, there will be one key generated to add other nodes into the swarm as a worker. Copy and run that key on the rest of the servers.

    Now you can see both my worker nodes joined swarm-manager as a worker.

  6. To check the swarm node status, go to the master node and type:

    docker node ls

  7. Now, on Manager Node we will create a service,

    sudo docker service create --name django-todo-cicd --replicas 3 --publish 8000:8000 subho4563/django-todo-cicd

  8. Now run

    sudo docker service ls

  9. now go to worker nodes and run

    docker ps

  10. Now, this service will be running on all three nodes. To check this, just grab the Ip Address of any of the nodes followed by port 8000. As

    <Any_ip_of_3_vms>:8001

  11. Now if you want to remove any node from the environment then run

    sudo docker swarm leave

    docker node ls ------in master node

    You will see one node status is down.

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxTHANK-YOUxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx