This project demonstrates how to integrate MySQL with Python using Docker. The project includes:
- 🛠️ Running a MySQL database inside a Docker container.
- 🔗 Connecting to this MySQL container from a Python application.
- 📦 Using Docker to build and run a Python container that interacts with MySQL.
Follow this guide to set up and execute the project successfully.
Before starting, ensure you have the following installed:
- 🐳 Docker: Required to create containers for both MySQL and Python.
- Git (optional): Needed if you wish to clone the repository.
- 🐍 Python 3.x: For the Python application to interact with the MySQL container.
First, clone the repository to your local machine:
git clone https://github.com/Badal2456/MySQL-Python-Docker-Integration.git
cd your-repo-name
To run MySQL in a container, pull the official MySQL image from Docker Hub:
docker pull mysql
This command downloads the MySQL image, which we'll use to create the MySQL container.
Optional: If you want to write the Python code yourself instead of using the provided code, create a Python file (e.g., demo_sql.py
) with the following structure:
import mysql.connector
def create_connection():
connection = mysql.connector.connect(
host="mysql_container_ip",
user="root",
password="root",
database="userinfo"
)
return connection
def main():
connection = create_connection()
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS usernames (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))")
print("Connected to MySQL and ensured table exists!")
if __name__ == "__main__":
main()
This simple script connects to the MySQL container and ensures a table named usernames
exists.
Start a MySQL container by running the following command:
docker run -d \
--env MYSQL_ROOT_PASSWORD="root" \
--env MYSQL_DATABASE="userinfo" \
--name mysql_container \
mysql
Here’s what each part of the command does:
-d
: Runs the container in detached mode (background).--env MYSQL_ROOT_PASSWORD="root"
: Sets the root password for MySQL toroot
.--env MYSQL_DATABASE="userinfo"
: Creates a database nameduserinfo
.--name mysql_container
: Names the containermysql_container
.mysql
: Specifies the MySQL image to use.
This will start the MySQL container and create a database named userinfo
.
To get the IP address of the running MySQL container, run:
docker inspect mysql_container
Look for the NetworkSettings
section and copy the IP address. Update your demo_sql.py
file to use this IP in the host
parameter of the create_connection()
function:
host="mysql_container_ip" # Replace with the actual IP
Create a Dockerfile
for your Python application with the necessary configuration. Once the Dockerfile is ready, build the Python image using the following command:
docker build -t pyapp .
After building the image, run the Python container with the following command:
docker run -it --rm pyapp
The script will now run, connect to the MySQL container, and perform database operations.
If you prefer not to build the Python image yourself, you can directly pull the prebuilt image from Docker Hub:
docker pull badal07/pyapp
docker run -it --rm badal07/pyapp
The application will now connect to the MySQL container, and you can start interacting with it.
When the Python application starts, you’ll be presented with the following options:
-
➕ Add a Name
- Choose
1
to add a name to the MySQL database. - The application will prompt you to enter a name, and it will be inserted into the
usernames
table in theuserinfo
database.
- Choose
-
📋 Show All Usernames
- Choose
2
to display all the names stored in the MySQL database. - The application will fetch and display all usernames from the database.
- Choose
-
❌ Quit
- Choose
3
to exit the application.
- Choose
This project showcases the integration of MySQL and Python using Docker, making it easy to run a portable, containerized application. Follow the steps above to set up the environment and start exploring the application’s features.
🎉 Happy Coding! 🚀