Databases with Docker
Spinning up Databases within seconds…..
Spinning up Databases within seconds…..
Setting up different databases for projects is a common thing to do when working on different projects , The hassle for setting up local instances of these databases can be easily over come using Docker .
I’ve used multiple databases from my projects , Maintaining these databases in one machine is a tedious job.
This problem can be solved using Docker. I’ve created a group of commonly used databases as a github repo ,that is how we will be setting them up with a single command.
Example of spinning up a mysql instance with docker.
docker run --name mysql-docker -e MYSQL_ROOT_PASSWORD=pass -d mysql:latestThis is cool but there rises few issues with persisting database and finding the right command for all the databases and keeping track of these docker images.
Docker Compose (The Problem solver)
We can use docker volumes to persist the current state of our database. By this we dont have to redeploy our schemas and prevent data loss after the stopping of docker images.
We found the solution ? is that it…….
Noooooo!!! , Typing out long docker commands is error prone , So we can further simplify the process using docker compose . Which can be used for writing all our configuration in a single file and starting our containers using one command.
Configuration File
Let consider my-sql as an example for the sample database
#docker-compose.yaml
version: '3.3'
services:
db:
image: mysql:8.2.0
environment:
MYSQL_DATABASE: 'db'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'user'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : <MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- ./cache:/var/lib/mysqlThis is an example configuration file which can be used for spinning up local sql docker instances .
We can have multiple services acting as one single entity with docker compose i.e the services: part can have multiple services , The way in which we will be working with the database is binding the database port to our machines port and typically use it as if the database is running in our local machine.
We can also configure the environment variables for further customization , Then comes the fun part — volumes , This will tell docker to merge the cache folder in the current directory to the /var/lib/mysql directory on the docker instance in this way whenever we are restarting our docker images we can just continue from where ever we left without any additional step.
Lets Get it Runninnn….
After all these step it’s time to see our docker image in action…
docker-compose up #Use the configuration file to spin up the docker imageIf all your configuration are correct you will see your database instance running.
We are not running our container on detached mode . If you do so, it will be in backgroud as long as the machine runs.
We are successfully running our database as a docker container with data persistance……
Can we do more?
The above mentioned steps are common to all the databases and the programming language you use , I mostly use NodeJs as my language of choice , if you are also doing so , you can add it as a convenient script to start the database server whenever needed.
"scripts": {
...you_existing_config,
"db": "docker-compose up"
}This is a part of my package.json , if the cache directory is conflicting with your build system , you can also relocate it to your directory of choise.
With this we have successfully added docker to our workflow with the help of docker-compose.
I’ve curated a set of popular databases and its configuration files , Do check it out in github . If you are not able to find your database of choise , A PR is highly appreciated….
Happy Coding…….
Comments ()