1. Deploying a Flask application using the Docker-Compose concept.
First, let's update our System and install docker.
Now you can validate that docker is running as well as its version
docker --version
systemctl status docker
Now let's install docker-compose
sudo apt install docker-compose
Now let's clone our GitHub repo.
git clone https://github.com/subho45631/flask-app-ecs.git
Now we can see our flask directory and its contents, now we need to create a docker-compose file
vim docker-compose.yaml
Start the container application in detached mode.
docker-compose up -d
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)
First Clone your GitHub repo:
git clone https://github.com/subho45631/flask-calculator.git
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
docker build . -t flask-cal-app
docker images -------to check image build
No run it as a container
docker run -d -p 3000:3000 flask-cal-app:latest
Now allow port no:3000 in your ec2 instance by editing the inbound security rule
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).
Follow the steps:
First of all, go to the AWS portal and create three new instances. Named:
· Swarm-manager
· Swarm-worker1
· Swarm-worker2
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
Now, Open the “Swarm Manager” node and run the command,
“sudo docker swarm init”
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
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.
To check the swarm node status, go to the master node and type:
docker node ls
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
Now run
sudo docker service ls
now go to worker nodes and run
docker ps
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
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