Database to go! The perfect database for developer
Tags: webdev, database, productivity, docker
Published: 2023-11-08 17:30:05

---
title: Database to go! The perfect database for developer
published: true
date: 2023-11-08 12:50:00 UTC
tags: webdev,database,productivity,docker
canonical_url: https://www.frankysnotes.com/2023/11/database-to-go-perfect-database-for.html
---

When building a new project, we don't need a big database that scales and has lots of data, but we do still need some kind of data source. Of course, it is possible to fake it and have some hardcoded value returned by an API but that takes time to create and it's not a database. In this post, I want to share a solution to have a portable, self-healing, disposable, disconnected database that doesn't require any installation.

The solution? Put the database in a container! It doesn't matter what database you are planning to use or on which OS you are developing. Most databases will have an official image available on [Docker Hub](https://hub.docker.com/) and Docker runs on all platforms. If you feel uncomfortable with containers, have no fear, this post is beginner-friendly.


*This post is part of a series where I share my experience while building a Dungeon crawler game. The code can be found on [GitHub](https://github.com/FBoucher/2d6-dungeon-app).*


## The Plan

Have a database ready at the "press of a button". By "ready", I mean up and running, with data in it, and accessible to all developer tools.

## Preparation for the Database

We need a script to create the database schema and some data. There are many ways to achieve this. A beginner-friendly way is to create an empty database and use a tool like [Azure Data Studio](https://learn.microsoft.com/azure-data-studio/download-azure-data-studio?WT.mc_id=academic-111465-frbouche) to help create the SQL scripts. Doing it this way will validate that the script works.

The Docker command to create the database's container will change a little depending on the database you are using but here what's a MySQL one look like:

```
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD='rootPassword' -p 3306:3306 -d mysql 

```

Where _some-mysql_ is the name you want to assign to your container, _rootPassword_ is the password to be set for the MySQL root user, and `-d` means that the container will run detached. The `-p` option is used to map the port 3306 of the container to the port 3306 of the host. This is required to be able to connect to the database from the host.

[![output of the docker run command](https://blogger.googleusercontent.com/img/a/AVvXsEiQYJ324kKLX9MQxf22foUrbRk3AE4BGWfwX71h-72Ux-voHE_HruGLTfGEpawg3ZuoXL852X8xtRYhZIRU5wADw-oN4oxrBxfiwm2UaXCB6-pMk9bh9Gq6_gIZaae_S3j5n00I30SkdTqHdKEBXBL4ILc9itIFWFH61SdvCv5aJgwG9cGyFVXwS9emyes=w530-h298)](https://blogger.googleusercontent.com/img/a/AVvXsEiQYJ324kKLX9MQxf22foUrbRk3AE4BGWfwX71h-72Ux-voHE_HruGLTfGEpawg3ZuoXL852X8xtRYhZIRU5wADw-oN4oxrBxfiwm2UaXCB6-pMk9bh9Gq6_gIZaae_S3j5n00I30SkdTqHdKEBXBL4ILc9itIFWFH61SdvCv5aJgwG9cGyFVXwS9emyes)

Now, a MySQL server is running inside the container. To connect to the server with Azure Data Studio use the extension _MySQL extension for Azure Data Studio_. Microsoft has a [QuickStart: Use Azure Data Studio to connect and query MySQL](https://learn.microsoft.com/azure-data-studio/quickstart-mysql?WT.mc_id=academic-111465-frbouche) if needed. Create a new connection in Azure Data Studio, then create a database (ex: 2d6db).

[![Create a new connection in Azure Data Studio](https://blogger.googleusercontent.com/img/a/AVvXsEj7y2FEL3qs95s5P3ywcFA4WaXCcfehZKpVZ2dSKeCZZTZMmjxUwBjwSNkmzsN9zDQuDQawjM_GHe2O0ngvKbhbBduwx2wOSzrGp2GCMpMbK8CQYwjlm3KnJ8jsjPa2b4BtfAAgV8y-CrEesYOs1TgKtNocnvc7TrmxF-fgUqAIp4MybxIa8mi0IA8qzCM=w457-h374)](https://blogger.googleusercontent.com/img/a/AVvXsEj7y2FEL3qs95s5P3ywcFA4WaXCcfehZKpVZ2dSKeCZZTZMmjxUwBjwSNkmzsN9zDQuDQawjM_GHe2O0ngvKbhbBduwx2wOSzrGp2GCMpMbK8CQYwjlm3KnJ8jsjPa2b4BtfAAgV8y-CrEesYOs1TgKtNocnvc7TrmxF-fgUqAIp4MybxIa8mi0IA8qzCM)
  
You can use the MySQL command-line tool if you prefer, but Azure Data Studio offers a lot of help when you are not that familiar with SQL. You can even use the Copilot extension and ask it to write the SQL statement for you. It's pretty good!

If you want to learn more about this, check the Open at Microsoft episode: [Copilot is now in Azure Data Studio and this is how it can help you!](https://learn.microsoft.com/en-gb/shows/open-at-microsoft/copilot-is-now-in-azure-data-studio-and-this-is-how-it-can-help-you?WT.mc_id=academic-111465-frbouche) to see it in action.

{% youtube lAlqWKDODDE %}

It's fantastic to generate a first draft of the _create_ statements and to make queries.

[![Copilot writing SQL](https://blogger.googleusercontent.com/img/a/AVvXsEh13h3Uo_EkzIVaKicwE793ZJU6yD8ziJ92ws_4PzEJt8Uw6qLbwJkZCZvAhhnEuY4f2LHKz5JRKEji-hRpXYvlxAGfkUVn-QsVXpYeEJoG9YnZXtX-0SshrINiXCKNt2jWqszj3M5pVALp4E3L2r4OYiRurWYUfXNEQxvlWgmGJs4nl5Ktmi02Yh_oRDc=w522-h180)](https://blogger.googleusercontent.com/img/a/AVvXsEh13h3Uo_EkzIVaKicwE793ZJU6yD8ziJ92ws_4PzEJt8Uw6qLbwJkZCZvAhhnEuY4f2LHKz5JRKEji-hRpXYvlxAGfkUVn-QsVXpYeEJoG9YnZXtX-0SshrINiXCKNt2jWqszj3M5pVALp4E3L2r4OYiRurWYUfXNEQxvlWgmGJs4nl5Ktmi02Yh_oRDc)

Let's create two SQL scripts. The first one will be to create the schema with all the tables. The idea here is to write the script and execute it to validate the results. Here is an example creating only one table to keep the post simple.

```
-- schema.sql

CREATE TABLE IF NOT EXISTS 2d6db.rooms (
  id int NOT NULL AUTO_INCREMENT,
  roll int DEFAULT 0,
  level int DEFAULT 1,
  size varchar(10) DEFAULT NULL,
  room_type varchar(255) DEFAULT NULL,
  description varchar(255) DEFAULT NULL,
  encounter varchar(255) DEFAULT NULL,
  exits varchar(255) DEFAULT NULL,
  is_unique bool DEFAULT false,
  PRIMARY KEY (id)
);

```

Now that there are tables in the database, let's fill them with seed data. For this, the second SQL script will contain _insert_ statement to populate the tables. We don't need all the data but only what will be useful when developing. Think about creating data to cover all types or scenarios, it's a development database so it should contain data to help you code.

```
-- data.sql

INSERT INTO 2d6db.rooms(roll, level, room_type, size, description, exits, is_unique)
VALUES (2,1,'Empty space', 'small','There is nothing in this small space', 'Archways',false);

INSERT INTO 2d6db.rooms(roll, level, room_type, size, description, exits, is_unique)
VALUES (3,1,'Strange Text', 'small','This narrow room connects the corridors and has no furniture. On the wall though...', 'Archways',false);

INSERT INTO 2d6db.rooms(roll, level, room_type, size, description, exits, is_unique)
VALUES (4,1,'Grakada Mural', 'small','There is a large mural of Grakada here. Her old faces smiles...', 'Archways',true);

```

Note: You can now stop the database's container with the command: `docker stop some-mysql`. We don't need it anymore.

## Putting All the Pieces Together

This is when the magic starts to show up. Using a _Docker Compose_ file, we will start the database container and execute the two SQL scripts to create and populate the database.

```
# docker-compose.yml

services:
  
  2d6server:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: '2d6db'
      MYSQL_ROOT_PASSWORD: rootPassword
    ports:
       - "3306:3306"
    volumes:
      - "../database/scripts/schema.sql:/docker-entrypoint-initdb.d/1.sql"
      - "../database/scripts/data.sql:/docker-entrypoint-initdb.d/2.sql"

```

The `docker-compose.yml` files are in YAML and usually are used to start multiple containers at once, but it doesn't need to. In this scenario, the file defines a single container name **2d6server** using just like the previous Docker command and MySQL image and configuration. The last command `volumes` is new. It maps the path where the SQL scripts are located to `/docker-entrypoint-initdb.d` inside the container. When MySQL starts it will execute the files in that specific folder in alphabetic order. This is why the scripts are renamed `1.sql` and `2.sql`, as the table must be created first.

Do get the database up and ready, we will execute the `docker compose up`.

```

# start the database
docker compose -f /path_to_your_file/docker-compose.yml up -d 

# stop the database
docker compose -f /path_to_your_file/docker-compose.yml down -d 

```

By default, the command looks for a _docker-compose.yml_ file. If you have a different name use the argument `-f` to specify the filename. Optionally, to free the prompt you can pass the argument `-d` to be in _detached mode_.

[![Docker Compose commands](https://blogger.googleusercontent.com/img/a/AVvXsEi78uMvy5pap6xq87lgOtUF063hXgEk8KNBq3wP4GsMPd00FtzujhKM7DJUJYu30ycF6CMFac5Z2gjQJ9MQZJQdr6FWga-9aP6Xu0yh7f5pbxzQcU2qm4sDb7e6gUXNjdaRXeuics6ezzcrTFZFwtNAdnOOoRBpR7A8yO2Hs8KUqueK6fbgv3ay5leiHvc=w531-h171)](https://blogger.googleusercontent.com/img/a/AVvXsEi78uMvy5pap6xq87lgOtUF063hXgEk8KNBq3wP4GsMPd00FtzujhKM7DJUJYu30ycF6CMFac5Z2gjQJ9MQZJQdr6FWga-9aP6Xu0yh7f5pbxzQcU2qm4sDb7e6gUXNjdaRXeuics6ezzcrTFZFwtNAdnOOoRBpR7A8yO2Hs8KUqueK6fbgv3ay5leiHvc)
  

When you are done coding and you don't need the database anymore, execute the `docker compose down` command to free up your computer. Compared to when the server is installed locally, a container will leave no trace; your computer is not "polluted".

When you need to update the database, edit the SQL script first. When the scripts are ready, execute the `docker-compose restart` to get the database refreshed.

## To Conclude

Now, you only need to execute one simple command get a fresh database, when you want. All the developers don't need to have a database server installed and configured locally. And you don't need to be worried when deleting or modifying data, like when using a shared database. After cloning the repository all developers will have everything they need to start coding.

In a next post, I will share how I used Azure Data API Builder to generate a complete API on top of the database using the same _docker compose_ method.

## Video version!
If you prefer watching instead of reading here the video version of this post!
  
{% youtube Y114CBEnPEU %}

### References

- [Docker run reference](https://docs.docker.com/engine/reference/run/#detached-vs-foreground)
- [Docker Compose overview](https://docs.docker.com/compose/)
- [Azure Data Studio](https://learn.microsoft.com/azure-data-studio/download-azure-data-studio?WT.mc_id=academic-111465-frbouche)
- [MySQL container](https://hub.docker.com/_/mysql)