๐Ÿš€ Deploying a Simple Flask App with Docker

Simple Project on Docker ๐Ÿณ

ยท

2 min read

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

Did you find this article valuable?

Support Divya_satpute's_blog by becoming a sponsor. Any amount is appreciated!

ย