Docker Project To Create Container for Flask App

I am a DevOps Engineer with an aim to learn and contribute to the Tech World!!
📝 Blogging and Sharing: On this platform, I'll be sharing insightful articles, tutorials, and tips on all things DevOps. Expect deep dives into CI/CD best practices, IaC patterns, containerization strategies, cloud optimization, and much more. Let's learn and grow together!
Agenda Of the Project
- To create a docker file for the flask app and then will build a docker image and then will create a container from that docker Image for a flask app
Steps For the Project
- So first we will take the code for the flask app and put it inside the directory flask-app
from flask import Flask
# Create a Flask app instance
app = Flask(__name__)
# Define a route and associated function
@app.route('/')
def hello():
return 'Hello, Flask!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Now we have to create a dockerfile
vim Dockerfile
#Getting base Image for Python flask app
FROM python:3.9
#Creating Working Directory for flask app
WORKDIR app/
#Copying code from system and put it inside the working directory of container
COPY app.py .
#Install required libraries
RUN pip install Flask
#RUN the application
CMD ["python","app.py"]

- Now we have build the docker Image from the above docker file and for that we are going to use below command
$docker build . -t flask-app
#Here we are asking docker to build an image for the docker file
#. means to create docker image for the current directory docker file
#-t is used for giving a tag or name for our docker image

- also we can check whether the docker image is created or not and for that we need to use below command and it will show the list of docker images
$docker images
- Now we have to create a docker container from the docker image that we have build in the above steps and for that we are going to use below command
$docker run -d -p 5000:5000 flask-app:latest
#Here -d means to run the process in background as in detached mode
#-p means to publish
#5000:5000 means we are binding the system port number with
#the docker container port number

Now we can also check for the container by using the below command
$docker ps
#It will show the docker containers

Now in order to check for our container-deployed app in the web browser just copy the public IP of your instance
And paste :5000 at the end and see the application running in the browser
For example : http://3.94.119.5:5000/
If it fails then you have to edit the inbound Rules for the security group of your aws ec2 instance
Adding inbound rules for all the ip so that they can access it then save it

- Now if you again refresh the below url
- Then you can see the container containing code application output

Thanks ,
Amrit Manash
Happy Learning 😊⭐

