How to Setup a MySQL Server in Docker for Your Python Project

Need a MySQL server running in Docker for your Python project?

Need to set up a SQL server for your Python setup? You don’t need to install it on your system. Use the prebuilt servers coming to Docker. Yes, you can use a prebuilt Docker image with a MySQL server for your Python project.

In this tutorial, you will learn how to setup it up in Docker, connect to it, some advantages of using Docker for this, and how to have your Python code use it.

When you need a MySQL server, most think they need to install one. But luckily you don’t need that if you have Docker running.

Docker enables you to create a MySQL server in a few seconds. Wipe it and start all over, which is often necessary while testing. Also, you can have multiple MYSQL servers running simultaneously.

But how do you do all that? In this tutorial, you will learn how

  • Get get Docker
  • Setup a MySQL server in Docker
  • How to prepopulate it with tables
  • How to connect to it with Python – adding rows of data and reading the data.

Step 1: Install Docker Desktop if you don’t have it

Docker enables you to run a lot of services locally on your machine with simple commands. Also, they are only run in Docker so no real installation is done on your machine. This makes it very handy. This is what we will do with the MySQL server in this tutorial.

First of all, you need to make sure you have Docker installed.

You can install Docker Desktop and it will automatically install all you need to manage all Docker tools need.

Go to Docker official download and get Docker Desktop for your OS (here).

Follow the installation instructions and you are ready to go.

  • Linux Notice. Please notice that if you use Linux there is no Docker Desktop you will need to use command lines when we use the Desktop. For the most part, we only use it to see the containers and see what is running. This can be done from a command line as well.

You can launch Docker Desktop, which will start the Docker Daemon in the background.

Step 2: Clone the repository with the setup

To build the Docker image and set up the MySQL server, we need a few files.

You can get all the files from GitHub here.

The Dockerfile contains the recipe for the image we will build.

FROM mysql/mysql-server
    MYSQL_ROOT_PASSWORD=password \
ADD schema.sql /docker-entrypoint-initdb.d

This file describes all Docker needs to build an image, which you can run afterward.

The first line contains the image from mysql/mysql-server which is the official MySQL server image (source).

Then it creates a Database and adds a schema.

Let’s explore the schema.

  `OrderTime` datetime,
  `Item` varchar(100) NOT NULL

Here you can change it for your needs. This one creates a simple database with one table Orders, with two columns OrderTime and Item.

Step 3: Build the Docker image and run it

You can build the Docker image by the following command.

docker build -t local-mysql .

And run it as follows.

docker run -dp 3306:3306 local-mysql

If you want to learn more about Docker for Python developers also read this guide.

This should start the MySQL server in your Docker Desktop.

Step 4: How to call your Docker MySQL server from Python

The file will insert a lot of rows into the MySQL database (the server).

import csv
from datetime import datetime
import mysql.connector

mydb = mysql.connector.connect(
cur = mydb.cursor()
cur.execute("USE DB")

data_file = 'orders.csv'
with open(data_file, "r") as f:
    csv_reader = csv.DictReader(f)
    records = list(csv_reader)

for record in records:
    order_time = record['order_time']
    date_obj = datetime.strptime(order_time, '%Y-%m-%d %H:%M:%S.%f')
    item = record['item']
    sql_stmt = f"INSERT INTO Orders(OrderTime, Item) VALUES('{date_obj}', '{item}')"

Please notice, that you need to have mysql-connector-python installed.

pip install mysql-connector-python

The code opens a CSV file orders.csv and inserts all the rows into the database.

To see what is in the database run the

import mysql.connector

mydb = mysql.connector.connect(
cur = mydb.cursor()
cur.execute("USE DB")
sql_stmt = f"SELECT * FROM Orders"
response = cur.fetchall()
for row in response:
    print(row[0], row[1])

This will show all the rows in the MySQL database running in your Docker.

Step 5: Understanding it all and a few notes

You can stop and start the local-mysql container and the content will be kept.

But if you delete it, and re-run the command, all the content in the database will be lost.

Why bother using Docker and not install MySQL on your computer?

Well, you often need to produce code and be able to reproduce the setup on another machine. When you set up something locally, you forget what you did and some might be system specific. Hence, when you need to set up production, it often needs to be done differently. This is where Docker is great. It does it all, it keeps the steps, and it can be reproduced on all machines with Docker running.

As a professional Python developer, you need to start using Docker for all these kinds of things. This will enable you to work as the highly-paid developers do.

Be sure to learn about creating a Python REST API using your Docker MySQL server.

Want to learn more?

Get my book that will teach you everything a modern Python cloud developer needs to master.

Learn how to create REST API microservices that generate metrics that allow you to monitor the health of the service.

What does all that mean? 

Don’t wait, get my book and master it and become one of the sought-after Python developers and get your dream job.

Learn Python


  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment