๐ Deploying a Simple Flask App with Docker
Simple Project on Docker ๐ณ
Creating a Dockerized Flask app is a great way to ensure your application runs consistently across different environments. In this guide, we'll walk you through the process step-by-step using emojis for a fun twist! ๐
๐ Prerequisites
Before we start, make sure you have the following installed:
๐ Python
๐ณ Docker
๐ป A code editor (like VS Code)
๐ป EC2 Machine (AWS)
๐ Project Structure
๐ ๏ธ Step-by-Step Guide
1. Set up EC2 machine and log-in using Gitbash and Install docker on EC2 machine
2.๐ฆ Setting Up Your Flask App
from flask import Flask
helloworld = Flask(__name__)
@helloworld.route("/")
def run():
return "{\"message\":\"Hey there python\"}"
if __name__=="__main__":
helloworld.run(host="0.0.0.0", port=int("3000"), debug=True)
3. ๐ Listing Dependencies
Next, list your dependencies in requirements.txt
.
requirements.txt:
flask
4. ๐ณ Creating the Dockerfile
Now, create a Dockerfile
to define your Docker image.
Dockerfile :
FROM python:3-alpine3.15
WORKDIR /app
COPY . /app
RUN pip install -r requirement.txt
EXPOSE 3000
CMD python ./index.py
5. ๐๏ธ Building the Docker Image
With your Dockerfile
in place, build your Docker image.
docker build -t divyasatpute/hey-python-flask:latest .
6.๐ข Running the Docker Container
Once the image is built, run it using Docker.
docker container run -d -p 3000:3000 divyasatpute/hey-python-flask:latest
๐ Verify the Deployment
Open your web browser and navigate to http://localhost:
3000
. You should see "Hey there python!" ๐
Now Everything is working fine then our last step is only pending which is nothing but PUSH ON OUR DOCKERHUB
7 .Push Image in our DockerHub Account
docker push divyasatpute/hey-python-flask:latest
Make sure that you can replace your own username and the things whenever its needed :
๐ Summary
โ๏ธ Created a simple Flask app
๐ Listed dependencies in
requirements.txt
๐ณ Defined a Dockerfile
๐๏ธ Build the Docker image
๐ข Run the Docker container
Congratulations! You've successfully Dockerized your Flask app! ๐ฅณ Now your app can run consistently in any environment.
Here I am showing all the results you can also Refer :
Feel Free to ask or comment if you are facing some issue