MySQL inside Docker

Spread the love
  • 10

In the previous article, we learnt how Spring boot application can be packaged inside a docker container. The containerization approach helps to quickly spin up an environment and destroy it when not needed. This approach is not just limited to Programming languages but it can be easily be extended for Databases too! Most of the databases including MySQL, PostgreSQL can be easily be containerized. The configuration can be tuned as per application requirements and developers have total freedom to test the changes without impacting the centralized environment. In this article, we will focus on how MySQL database can be containerized. If you do not have Docker setup yet, please refer to this article for docker installation.

Getting started with Container
To quickly get started, let’s pull MySQL container image. The intention is to get familiar with MySQL environment variables and connection details. Execute the following command at the console

docker pull mysql

Note that above instruction will only pull the image in local docker repository. Execute docker image ls and you should see following output.

MySQL image list

MySQL image list

You can now start MySQL in Docker using

docker run –name hello_mysql –env=”MYSQL_ROOT_PASSWORD=hello” -p 3306:3306 -d mysql

This will start MySQL inside Docker container. Note that

  1. we are mapping host port to container port using -p.
  2. Additionally -d has been specified to run the container in the background instead of blocking the console.
  3. It is also necessary to pass MYSQL_ROOT_PASSWORD as an environment variable to perform initial setup of the database.

To check if MySQL has successfully started in the container, execute following command in the console.

docker container logs hello_mysql

If you see the following statement, it means that MySQL has successfully started

Mysql log

Mysql log

You can now use MySQL workbench to connect the database. If workbench is not already installed in your machine, click here for Linux OR click here for Microsoft Windows.

Use following settings to connect the database

Port: 3306
User ID: root
Password: hello (Note this is the same password that we supplied while starting mysql container)

Once you are connected, you should be able to execute the queries against the database.

MySQL workbench

MySQL workbench

Let’s access the MySQL container shell

docker container exec -it hello_mysql bash

You should now see a prompt like following

MySQL Console

MySQL Console

You can now connect to the database using the following command

mysql -u root -p

This will prompt for a password. Enter hello at the prompt and press enter. You should now be able to access MySQL prompt and can query the database.

While we are able to access the MySQL database using Workbench and console, the data stored in Database will be lost as soon as you will stop or kill the container. This is the default behaviour of the container and useful to quickly spin up an environment and destroy it. But this behavior is not good for storing persistable data. In the next article we will look at how MySQL data can be persisted across container restart.

Leave a Reply

Your email address will not be published. Required fields are marked *