Run Multiple Databases (MySQL, Oracle, MSSQL, PostgreSQL, MongoDB) in Single Machine Using Docker (and Vagrant)

By | June 23, 2019

Bismillahi-r-Rahmani-r-Rahim (In the name of Allah, the most Compassionate, the most Merciful)
Assalamu alaikum wa rahmatullahi wa barakatuh (May the peace, mercy, and blessings of Allah be with you)

This is the ad hoc tutorial on how to setup and run multiple databases simultaneously (MySQL, Oracle, MSSQL, PostgreSQL and MongoDB) in single machine using Docker (and Vagrant) approach.

Below content is complementary to the video tutorial above.

Intro

Sometimes we may need quick database for different reasons (development, testing etc.). Also it may be painful to dedicate new machine for this and go through the installation process. Imagine how much time and resources are needed to setup and run the new Oracle or MSSQL database. Or what if we develop multi-database application and there are no enough resources and motivation to setup each DB separately?

Good news is that we have an elegant, neat and quick solution for this issue: Docker. Docker is an isolated container environment with pre-installed software which is similar to virtual machine. Docker is extremely easy to configure and deploy which is good for sharing and portability. It is possible to run multiple databases in a single machine using single docker configuration file. I will show you how to run 5(+1) databases – MySQL, MSSQL, Oracle, PostgreSQL and MongoDB in single CentOS machine with just 3GB (or even less) of RAM.

Requirements

To achieve our goal we need any machine (Linux, Windows etc.) with docker environment enabled. In this tutorial i will show how to setup docker environment in blank CentOS7 VM. You may check my other tutorial on how to setup blank CentOS7 VM into VMWare Workstation Player if you do not have one. Additionally, i have provided Vagrant solution below that will install CentOS7 with Docker environment for you in just minutes.

Before installation edit /etc/sysconfig/selinux and set SELINUX=disabled. Docker installation instructions could be found in https://docs.docker.com. I will follow CentOS instructions provided in https://docs.docker.com/install/linux/docker-ce/centos/ in the video tutorial. If you have different OS please follow the corresponding instructions. Next, we will install docker-compose using instructions from https://docs.docker.com/compose/install/.

Setup and run all databases

We need below steps to setup and run multiple databases simultaneously in our machine:

  1. Install and enable docker and docker-compose using above resources.
  2. Create the necessary folder structure. Below directories will be used for data persistence to conserve the database data for later reuse. Without persistence the data will be lost when containers are destroyed. We will map these folders to the corresponding folders within the containers.
    mkdir /docker
    mkdir /docker/mysql /docker/mysql/data /docker/mysql/conf
    mkdir /docker/mssql /docker/mssql/data
    mkdir /docker/oracle /docker/oracle/data
    mkdir /docker/postgre /docker/postgre/data
    mkdir /docker/mongo /docker/mongo/data
    chmod 777 /docker/oracle/data # to avoid oracle permission issues
    cd /docker
  3. Create docker-compose.yml configuration file inside created docker folder with provided content in docker-compose.yml. Please watch the video above for the short explanation of the file content. Copy mysql_custom.cnf file to /docker/mysql/conf/mysql_custom.cnf.
  4. Run the following command to setup and start all the database containers. This command will pull database images from the official docker repositories (this may take longer time at the first run) and setup the corresponding database containers. In order to download oracle docker image you need to accept terms conditions in official docker repository page using Proceed to Checkout button then login with your docker account using docker login command in the shell.
    docker-compose up -d
    PS: download process may take some time at the first run.
  5. Finally, run the following command the check the status of the containers:
    docker ps -a

Useful commands

You may find other useful commands at https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes.

Finally, we have single machine with 3GB of RAM running 5+1 databases simultaneously and this is amazing.

Quick Vagrant Solution to setup CentOS7 VM with Pre-installed Docker Environment

  1. Download and install Vagrant and VirtualBox.
  2. Create new folder. Copy Vagrantfile and bootstrap.sh from repository to the new folder and adjust them if needed.
  3. Open the command line and move to the created folder.
  4. Run vagrant up and wait until the machine is up, this may take some time for the first run. PS: If you get network error just disable and enable VirtualBox Network Adapter given in error description and repeat the command.
  5. Run vagrant ssh in command line to access the CentOS VM ssh console. Run sudo passwd root to specify root password. Then run su root to change to current user to root. Run docker –version and docker-compose –version to make sure that the docker environment is up. Then run docker login (if you need to pull oracle docker image).
  6. Finally execute steps 2-6 from Setup and run all databases section above .
  7. If you want to learn more about Vagrant here is the introductory tutorial: https://www.youtube.com/watch?v=vBreXjkizgo

Important note: above should only be used for testing purposes, using this for production is strongly dis-recommended


Related resources: