Using MariaDB with Docker and docker-compose can be a convenient way to set up a development environment for your application. In this article, we will go through the steps of setting up a development environment with MariaDB and Docker.
👋 Check out our easy to use desktop GUI for SQL
Beekeeper Studio is a truly cross-platform SQL GUI with a clean, uncluttered interface. Write SQL, edit data, alter tables, and more!
Available for MacOS, Linux, and Windows.
Install Docker and docker-compose
To use Docker and docker-compose, you need to have them installed on your machine. If you don’t have them installed, you can follow the instructions on the Docker website to install them.
Create a docker-compose.yml file
The next step is to create a docker-compose.yml
file in the root directory of your project. This file will define the containers that you want to run as part of your development environment. Read more about Docker compose on the Docker website
Here is an example docker-compose.yml
file that sets up a MariaDB container and maps permanent Docker ‘volume’ to the mysql data directory so data persists between restarts:
version: '3'
volumes:
data:
services:
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- data:/var/lib/mysql
ports:
- "3306:3306"
In this example, we are using the mariadb
image and setting several environment variables to configure the database. We are also exposing the default MariaDB port (3306) so that we can connect to the database from our application.
Spin up the containers
With the docker-compose.yml
file in place, you can use the following command to spin up the containers:
docker-compose up -d
This command will pull the necessary images and start the containers in the background. Run without -d
to start the container in the foreground. You can use the following command to see the status of the containers:
docker-compose ps
Connect to the database
To connect to the MariaDB database from your application, you can use the hostname db
and the port 3306
.
For example, in a Node.js application, you can use the following code to connect to the database on your machine:
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'user',
password: 'password',
database: 'mydatabase'
});
connection.connect();
Bootstrap with development data
The MariaDB image provides a way to ‘bootstrap’ the database with data on first start. To do this, map a local directory of .sql
files to the /docker-entrypoint-initdb.d
directory in the container.
version: '3'
volumes:
data:
services:
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- data:/var/lib/mysql
- ./dev/mariadb:/docker-entrypoint-initdb.d
ports:
- "3306:3306"
In this example I have a bunch of .sql
files in my local ./dev/mariadb
folder. When you start the container for the first time, MariaDB will run all the scripts in this directory (in alphabetic order). This is a great way to create tables and populate them with data.
You can see an example of this in use in the Beekeeper Studio repository. We use this exact feature to set up a testing environment.
Stop and remove the containers
When you are done developing and want to stop the containers, you can use the following command:
docker-compose down
This will stop the containers, but it will not delete the data stored in the MariaDB container. If you want to delete the data as well (which is useful if you’ve changed your bootstrap files), you can use the following command:
docker-compose down --volumes
That’s it! You have successfully set up a development environment with MariaDB and Docker.