2 minutes
Docker for Development Databases
During development I usually do a lot of database related tasks or tests that generate/modify data. If you consider the project based approach here at Cloudoki (meaning a considerable number of potential technologies in use) and add personal projects and Hackdridays to the mix, you’ll understand why this is a concern to me.
Because of this, I prefer to work on local instances and break all things if needed, without affecting the development process in the team (or my own).
While in the past I installed MySQL/PostgreSQL/others locally, I now containerize everything, which gives me less headaches and a lot more flexibility. Pretty sure this is not rocket science, and definitely the first guy to do it.
How I manage it
In the early stages, I simply created aliases on my shell to avoid writing the docker run
commands all the time.
Nowadays, I keep all those organized in a repository and everytime I need to use a new database (or a different version of some database I’m already using) I simply add to the commands file and run the simple setup script.
Here’s a sample of the commands file:
###################################
### Setup ###
###################################
BASE_DIR=$HOME/docker
DEFAULT_ROOT_PWD=mypassword
function d_databases_setup_volumes_dirs() {
mkdir -p $BASE_DIR/postgres
mkdir -p $BASE_DIR/mysql5.7
mkdir -p $BASE_DIR/mongo
}
###################################
### MySQL ###
###################################
function d_mysql_run() {
docker run --rm --name mysql5.7-docker -d \
-p 127.0.0.1:3306:3306 \
-v $BASE_DIR/mysql5.7:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=$DEFAULT_ROOT_PWD \
mysql:5.7
}
function d_mysql() {
docker exec --tty --interactive mysql5.7-docker mysql -uroot -p
}
###################################
### PostgreSQL ###
###################################
...
###################################
### MongoDB ###
###################################
...
BASE_DIR
is where I mount the data volumes, so they are persisted to my disk- I run
d_mysql_run
to start the MySQL container - I run
d_mysql
to use the terminal client in the container (avoids having to install the client locally)
Feel free to share your thoughts and ideas on this. I’m interested in other approaches and to continously improve mine!
Here’s the repository where I keep this stuff: https://github.com/s1moe2/docker_dbs
Originally posted on Cloudoki’s blog: https://blog.cloudoki.com/docker-development-databases/
336 Words
2020-02-07