Use a Single Docker Project to Host All Your Database Containers
Get Redis, SQL Server, MongoDB, and more up and running in docker in 15 mins!
Let me start by asking and answering a few quick questions:
- Have you installed a full SQL Server on your dev machine? Yes, I have.
- Have you installed Redis on your dev machine? Yes, I have.
- Have you installed all the other databases that you temporarily need on your dev machine? Yes, I have….
- Do you need them all running in the background on your dev machine, eating all the CPU and Memory, until the world ends? No, I do not…
We often go through tedious installation processes and configuration processes in order to get a database up and running. Lots of times though, we only need one test database briefly, a couple of days, or even just a couple of hours. It makes no sense to install it and have it running in the background all the time! It also makes no sense to install, uninstall, then install again when you need one…
The goal is to share how I overcome the challenge of having to install databases I need and then uninstall them, simply, by using docker.
First, we need Docker Desktop.
This step is straightforward. You can download Docker Desktop from docker website and install it.
Second, we will create one file to list all the databases we may ever need.
This list does not have to be exhaustive at the get-start. You can always add more or remove some down the road. The list itself is just a plain text file, plus some formatting requirements to make it a YAML file. If you haven’t used YAML, ignore the concept and just consider it a text file with tabs. Let’s call it “docker-compose.yml” and save the file on your dev machine.
Third, get ready for the glorious moments!
Open your favorite terminal, I am using Windows Terminal. We will run two commands. The first command to navigate to the directory where the docker-compose.yml file is stored. If your file is stored at “C:\sandbox\docker-compose.yml”, your directory would be “C:\sandbox”.
and the second command to get Redis, Mongo, and SQL Server all up and running in docker containers! The very first time you run this command may take a couple of minutes, depending on your internet speed, because it has to download the container images. The subsequent times you run this command should complete in seconds.
docker-compose -p "database-playground" up
Note: -p is short tag for “ — project-name”, which allows you to set a custom project name that will host all the containers.
Here is a screenshot of what Windows Terminal should look like if it succeeds.
Now, open you Docker Desktop, you should see a new project with three containers up and running. Notice my project name is “database-playground” as set previously.
You can test by clicking on the “>_” icon on Redis container, which shall take you into the container. Enter “redis-cli” to start using Redis CLI, and then you can create key-value pairs using “set” command and retrieve a value by key using “get” command. This indicates to me your Redis instance is up and running!
In about 15 mins time, we are able to get three database instances, Redis, SQL Server and Mongo, up and running. We also get the benefit of being able to easily turn them on and off either as a whole project or individually. Awesome!
Many thanks for reading! Hopefully this will save you some time when you have to work with multiple database types. Cheers!