This repository offers a fully containerized MySQL database setup using Docker, making it easier to deploy, manage, and scale your database environment. Whether you're a developer, a database administrator, or just starting to learn Docker, this guide covers everything you need.
This project allows you to run MySQL inside a Docker container, with automatic setup via an SQL script. Key features include:
- ✅ A fully configured MySQL instance running inside a Docker container 🐬
- ✅ Automatic creation of the database and tables 📦
- ✅ Sample data preloaded for quick testing 📝
- ✅ Easy connection to the MySQL database from any MySQL client 🔗
📂 dockerlab4
│── 📜 Dockerfile # Instructions to build the MySQL Docker image
│── 🗄️ database.sql # SQL script for setting up the database & table
│── 📖 README.md # Documentation for the projectEnsure you have the following tools installed:
- Docker → Install Here 🐳
- MySQL Client (optional, for connecting to the database externally)
To start the database inside a container, use the following commands:
docker build -t mysql-db .
docker run --name mysql-container -d -p 3306:3306 mysql-dbThis will:
- Build a Docker image for MySQL named
mysql-db. - Run a MySQL container named
mysql-container. - Expose MySQL on port 3306.
To access the database inside the container:
docker exec -it mysql-container mysql -u root -pPassword: root
To view the database and tables:
SHOW DATABASES;
USE student;
SHOW TABLES;
SELECT * FROM students;To stop the container:
docker stop mysql-containerTo remove the container:
docker rm mysql-container- Creates a database called
student. - Sets up a
studentstable withStudentID,FirstName, andSurname. - Inserts sample data, such as
John AndersenandEmma Smith.
- Uses the official MySQL image.
- Sets the root password to
root. - Copies
database.sqlto automatically initialize the database inside the container.
Looking to enhance this setup? Consider the following ideas:
- Expand the database schema → Add more tables to
database.sql. - Increase security → Create different user roles with unique passwords.
- Ensure data persistence → Mount a Docker volume for persistent database storage.
- Optimize the Dockerfile → Use a minimal base image to reduce the image size.
❓ MySQL container won't start? Check the logs:
docker logs mysql-container❓ Database changes not taking effect? Rebuild the container:
docker build --no-cache -t mysql-db .
docker run --name mysql-container -d -p 3306:3306 mysql-db