Introduction to Docker and Using Percona Server for MongoDB

PREVIOUS POST
NEXT POST

mongodb in docker containersIn this blog post, I’ll walk through using Docker containers and discuss the advantages of using them to run multiple versions of Percona Server for MongoDB (PSMDB) instances in the same server. This is particularly useful for local testing. Lots of companies have started to use containers for their applications, due to the advantages of this model, and due to their hassle-free deployment. I’ll show you how to configure variables for  MongoDB® when using docker containers. Hopefully, this will be a useful introduction that’ll help you to start working with Dockers yourself. Let’s get into the topic…

Experiments and testing…

It is always been fun to work with test cases for our databases. When I do that, I generally use VMs, Dockers or a test a machine – hmm! I know what you think… your boss should give you one if he wants you to solve problems by testing. There are still more things that you could try.  As we are working inside Percona, this might be MongoDB operators (see early release here:  https://www.percona.com/blog/2019/02/21/percona-server-for-mongodb-operator-0-2-1-early-access-release-is-now-available/) which is light weight packed MongoDB instance and these could be used with kubernetes + minikube or docker too to deploy and using it. You can expect more information about how this might work from our folks very soon!

What is Docker?

Here’s a basic Docker architecture diagram:

Docker-blogpost-diagram-vinodh.png

If you want to start your application quickly using lightweight software that packages up code and all its dependencies, then you need to go for containers. These use the host operating system and share the relevant libraries. In simple terms, a Docker engine is a platform that helps you to run multiple containers easily within the same host operating system. It allows you to quickly initialize databases & their applications and start to start to use them almost immediately. From the above diagram, you can see that we have a host OS, and Docker runs on that, sharing its kernel. We can start containers on top of Docker. For example, on my Macbook if I use Docker I can start using PSMDB 3.6 running with CentOS 7, Percona Server MySQL 5.7, Ubuntu Xenial etc all together. These containers don’t contradict each other with dependencies even though they have different configurations and you can customise them as per your requirement. 

Docker basics:

You should install Docker first. I’ve used this link for Mac OS. Then download the related image – which consists of the necessary software and its dependencies packaged together – as per your requirement. Now let’s see how to download a simple image and check how it works. Hmm… “hello world” is handy to test anything anywhere!

Check the list of images

This command tells you all the images available. These images are used to create a container and build your application. When you start a container, and if no related image exists, then the Docker engine will pull them from the Docker hub where lot of images are shared. An aside: you could build an image and share it there too if you wanted too. You can see lot of images below that I’ve downloaded and and have been using already for my own purposes. So don’t worry if you can’t find this exact list.

Check the list of all containers (running and stopped)

You can see the containers that I have created so far on my Mac:

Start PSMDB with the Docker

Now it’s time to start PSMDB with Docker. After the preliminary tests above, you’re good to go. Let’s start pulling the image for PSMDB 3.6.10:

Now start your database, a docker instance of PSMDB 3.6.10, using the image downloaded. Here’s the command:

List the running Docker containers and check whether the container you’ve created is running. You should see all those that are currently running:

Now login into the database as shown below:

You can run the Linux commands as follows:

If you would like to work with the command prompt within the Docker instance, then you can start bash and work through that shell:

Check mongod help

If you would like to change the parameters for MongoDB while starting the instance, you can check the options available to you as follows: start an instance, show the output, and destroy the container. In fact, this blog arose from a particular question from a community member: “How do I check and change mongodb settings in Docker?” 🙂

There are many ways to achieve that requirement and this is one way:

Custom mongod settings:

To start the new docker instance with custom MongoDB settings, you can start the container afresh like this:

The container that’s been started is not showing in the running containers process list. This suggests that there is some issue with the container.

Let’s check the log (the eagle eyed might have already noticed the problem in the command that I first used):

It’s the parameter name. Let’s use the correct parameter and start again.

Note: Even though there was an error, the docker command created the container with the name “psmdb36-custom” anyhow. So, to be able to use it again we need to remove it first and then restart with the same name:

Now check your setting in the mongod instance:

You’ll see that above we have used the option -v /mongodb/data:/var/lib/mongo -v /mongodb/log:/var/log/mongodb/  This allow you to access data files from a local machine and map that local storage as your docker  image partitions 

Available locally, the files exist in the file system like this, below. Be careful that you don’t alter them manually as they are db files:

Conclusion

I hope this post helps you to get going with Docker containers and to start using them straightaway. You can see how it could allow you to start different versions of MongoDB for the purposes of testing. As you start to explore, you’ll find you have other options available such as Docker swarm for maintaining Docker Engine clusters, and technologies, such as Kubernetes with Docker, that can help you to run entire applications and maintain nodes automatically. And I’ve already mentioned,  Percona likes to use MongoDB operators which are in early release at the time of writing this blog. Using a similar approach, you could test other databases such as Percona Server for MySQL.

Let me know your thoughts and share any tricks you might know that would help users to run Docker containers smoothly.

PREVIOUS POST
NEXT POST

Share this post

Comments (3)

  • Ruben Reply

    How is the correct way to shutdown the cointainer instance of mongodb?
    Many thanks

    May 30, 2019 at 7:47 am
    • Vinodh Krishnaswamy Reply

      Hi vRuben,
      You can do the following command which shutdown the mongod service as well and vice versa.

      docker stop mongodb-container

      June 13, 2019 at 8:33 am
      • Ruben Reply

        Many thanks Vinodh

        June 13, 2019 at 10:06 am

Leave a Reply