In today’s fast-paced development world, containerization has become a key skill for developers and system administrators. Docker, one of the most popular containerization tools, allows us to easily package and run applications in isolated environments, known as containers.
In this post, I’ll walk you through how I set up a MySQL database using Docker, accessed it through the container’s shell, and inserted some entries into the database.
Why Use Docker for Databases?
Running a database inside a Docker container can save time and effort, especially for development and testing purposes. Instead of installing and configuring MySQL directly on your system, you can quickly spin up a container and have a fully working database ready to use.
Some advantages of using Docker for database setup:
Portability: Easily move containers across different systems.
Isolation: Keep your database environment separate from your host system.
Version Control: Run specific versions of MySQL without worrying about conflicts with other applications.
Steps to Set Up MySQL in Docker
Let’s get started with setting up MySQL in a Docker container. Follow these steps to have your MySQL database up and running in no time!
1. Install Docker
If you don’t already have Docker installed, head over to the official Docker website and download the version suitable for your operating system.
After installation, confirm Docker is running by executing the following command in your terminal:
2. Pull the MySQL Docker Image
Docker provides an official MySQL image on Docker Hub. To pull the latest version of MySQL, run:
This will download the MySQL image to your local system. If you want to use a specific version of MySQL, you can replace latest
with the desired version tag (e.g., mysql:5.7
).
3. Run MySQL in a Docker Container
Once the MySQL image is downloaded, you can run a MySQL container. Run the following command to start a MySQL container:
Let’s break down the command:
--name DATABASE
: Specifies the name of the container (you can choose any name).-e MYSQL_ROOT_PASSWORD=TEST@123
: Sets the environment variable for the root password of MySQL.-d
: Runs the container in detached mode (in the background).mysql:latest
: Specifies the MySQL image version you want to use.
4. Access the MySQL Container
Once the container is running, you can access the MySQL shell by executing the following command:
This opens a bash shell inside the container. From here, you can access MySQL by typing:
Enter the password you set earlier (TEST@123
in this case) when prompted.
5. Create a Database and Insert Data
Now that you're inside MySQL, let’s create a new database and insert some data. Here are the steps:
- Create a database:
- Select the database:
USE SHIVAM_DB;
- Create a tables:
- Insert some data:
- Verify the data:
You should see the inserted entries in the result.
6. Exit the MySQL Shell and Container
To exit the MySQL shell, type:
exit;
To exit the container’s bash shell, simply type:
exit
Conclusion
With just a few simple commands, I was able to create a MySQL database inside a Docker container, interact with it through the MySQL shell, and insert some data. Docker makes it incredibly easy to set up and manage databases without worrying about complex installation processes.