Welcome to the Docker Lab Starter project! This repository is designed to help you get hands-on experience with containerizing an application using Docker. The goal is to take an existing application, test it locally, and then create a Dockerfile
to containerize the application.
By completing this assessment, you will:
- Understand the application's functionality by testing it locally.
- Learn to create a
Dockerfile
to containerize the application. - Run the application inside a Docker container.
- Use Docker Compose to manage a multi-container setup.
Before you begin, make sure you have the following installed:
- Python (version 3.9 or later)
- Pip
- Docker
- Docker Compose
First, clone the project to your local machine:
git clone https://github.com/mo-salah1998/Docker-Lab-Starter.git
cd Docker-Lab-Starter
To understand the application's functionality:
-
Navigate to the project directory:
cd Docker-Lab-Starter
-
Install the required Python dependencies:
pip install -r requirements.txt
-
Run the application:
python main.py
-
Open your browser and go to
http://127.0.0.1:5000
to interact with the application.
Take a moment to explore the app and understand what it does.
Your task is to create a Dockerfile
to containerize this application. Follow these steps:
-
Create a Dockerfile
- Inside the project directory, create a file named
Dockerfile
. - Write a
Dockerfile
to containerize the application. Here are some tips:- Use a Python base image (e.g.,
python:3.11-slim
). - Copy the application files into the container.
- Install dependencies using
pip
. - Expose port
5000
. - Set the command to run
main.py
.
- Use a Python base image (e.g.,
- Inside the project directory, create a file named
-
Build the Docker Image
Build the image using the following command:docker build -t docker-lab-starter .
-
Run the Container
Run the application in a container:docker run -d -p 5000:5000 docker-lab-starter
-
Test the Application in a Container
Open your browser and go tohttp://127.0.0.1:5000
to verify that the application runs correctly in a container. -
Push the Image to a dockerHub new repo
To enhance the application, you will persist tasks in a MySQL database.
-
Modify the Flask App
Update the Flask application (main.py
) to connect to a MySQL database and store tasks. Replace the in-memorytasks
list with database operations.Example changes:
- Use
mysql.connector
to connect to the database. - Fetch tasks from the database for display.
- Insert new tasks into the database.
- Use
-
Define
docker-compose.yml
Create adocker-compose.yml
file to manage both the Flask app and the MySQL database. -
Build and Start Containers
Use Docker Compose to build and start the containers:docker-compose up --build
-
Test the Application
Open your browser and go tohttp://127.0.0.1:5000
. Verify that tasks are stored in the MySQL database.
Once you have successfully containerized the application:
- Make sure your
Dockerfile
is included in the project directory. - Document your steps in a
README.md
file inside the project. - Push your changes to a GitHub repository and share the link.
- Refer to the Docker Documentation if you need help with Docker commands.
- Check for errors during the build or run process and debug accordingly.
- If you are new to Flask or Python, take some time to review the code in
main.py
. - Test database connectivity using the
mysql
CLI or a GUI client like MySQL Workbench. - Check for errors during container startup and debug logs with
docker-compose logs
.
By completing this assessment, you will gain hands-on experience in:
- Testing a Python web application.
- Writing and understanding
Dockerfile
syntax. - Building and running Docker containers.
- Containerizing a real-world application.
- Using Docker Compose for multi-container setups.
- Persisting data in a relational database.
- Managing real-world application workflows with Docker.
🚀 Happy coding & learning ! 🚀