Building and Deploying a Flask To-Do App with Docker, MongoDB, and Redis
In this article, we will guide you through creating a simple To-Do application using Flask, MongoDB, and Redis. We will then Dockerize the application to make it easy to deploy and run consistently across different environments. Let's start!
![](https://cdn.prod.website-files.com/646bd7f9373190ea43f9bff6/668d2c1f60c8c5638dc14cc9_image.webp)
Prerequisites
- Docker installed on your system
- Basic knowledge of Python and Flask
- Basic understanding of Docker and Docker Compose
Project Structure
Here’s the project structure we will follow:
Setting Up the Flask Application
First, create the Flask application.
app/app.py:
app/templates/index.html:
app/static/style.css:
app/requirements.txt:
like this
Create the Dockerfile
app/Dockerfile:
Create the Docker Compose Configuration
docker-compose.yml:
Building and Running the Docker Containers
Now that we have everything set up, let's build and run our Docker containers.
1. Navigate to the project root directory (todo-app
) and build the Docker images:
docker-compose build
2. Start the Docker containers:
docker-compose up
This command will build the Docker images for your Flask app, MongoDB, and Redis, and start the containers. You should see an output indicating that each service is starting.
3. Access the Flask application:
Open your browser and navigate to http://localhost:8080/
. You should see the To-Do application interface. Congratulations 🎉
Docker Commands Overview
Here are some basic Docker commands you might find useful:
Build Docker images:
docker-compose build
Start Docker containers:
docker-compose up
Stop Docker containers:
docker-compose down
Building and Pushing the Docker Image to Docker Hub
After creating your Dockerfile and docker-compose.yml, the next step is to build the Docker image and push it to your Docker Hub registry. Follow these steps:
1. Log in to Docker Hub:
docker login
You will be prompted to enter your Docker Hub username and password.
2. Build the Docker Image:
Navigate to the directory containing your Dockerfile (app
directory) and build the Docker image. Replace ghadeer99/todo-app
with your Docker Hub username and desired repository name.
docker build -t ghadeer99/todo-app:latest .
3. Tag the Docker Image:
docker tag ghadeer99/todo-app:latest ghadeer99/todo-app:v1.0
4. Push the Docker Image to Docker Hub:
docker push ghadeer99/todo-app:latest
If you tagged your image, push the tagged version as well:
docker push ghadeer99/todo-app:v1.0
Full Dockerfile and Docker Compose Workflow
Here's a full walkthrough of building, tagging, and pushing your Docker image:
1. Build the Docker Image:
2. Tag the Docker Image:
docker tag ghadeer99/todo-app:latest ghadeer99/todo-app:v1.0
3. Log in to Docker Hub:
docker login
3. Push the Docker Image:
Final Docker Compose Configuration
Make sure your docker-compose.yml
is configured to use the image you pushed to Docker Hub:
docker-compose.yml:
Running the Docker Compose
Now you can run your application using Docker Compose with the image from Docker Hub:
docker-compose up
This command will pull the image from Docker Hub and start the containers.