MongoDB in a Docker container with Authentication

In this tutorial we will learn to create a basic MongoDB instance that runs in a Docker container and uses authentication.

If you don’t want to follow along, you can just clone the image and use it as it is.

Technologies used: Docker & DockerhubBash scripting.

1. Setting up

I am running on MacOS Sierra, so the instructions will be for Mac users, but you should be able to follow along regardless of your OS. You should also have Docker installed and running.

2. Describing the Docker container

We will base our container on the official mongo docker container. We will also be instructing Docker to run a bash script when starting the container to create the database admin and application users, otherwise Mongodb will allow everyone to connect to it and see the data. The bash script is based on tutumcloud’s Github repo.

Now open the Dockerfile in your favourite editor and add the following content:

Nothing fancy in here. Just specifying the mongo version 3.4 as the base Docker image,  setting some Environment Variables for the mongo container (you can check the mongo documentation for what they are for), then instructing Docker to add the bash script files to the container (we’ll create these after) and lastly to run the run.sh when booting.

Now edit run.sh:

This file is responsible for starting the mongod service inside the container using the Environment Variables defined in the Dockerfile and at the end if there is no file called .mongodb_password_set (this file is created by the set_mongodb_password.sh script and it’s a flag to say that we’ve already created the users so don’t do it again in the future), it will run the set_mongodb_password.sh script described below.

Edit set_mongodb_password.sh:

I’ve added in-nene comments to explain what is happening at every step.

3. Building and deploying the Docker image

The Docker image is the repository containing the files we created at step 2 and that will be used as a template to create the Docker containers.

To create the image we use the docker build command (I tagged it with version 3.4 to match the mongo version):

Now that the image was created, I’m going to push it to DockerHub since it might be useful for other people or in case I’ll want to use it later.

First login to Docker hub:

Now push the image:

4. Testing and wrap-up

Now that the image was created, let’s try to run it. We’ll make visible port 27017 so we can connect to it and make sure everything is good.

Looks like everything ran and the users were created, let’s open a MongoDB client and test.

 

Persisting data:

If you want to persist the data (/data/db folder) when you rebuild that container you have a few options:

  • Bind the /data/db folder to a local folder (add the -v parameter to docker run):  docker run -it … -v /path/to/local/folder:/data/db alexpunct/mongo:3.4
  • Use Docker compose and volume_from in the docker-compose.yml file. More information here.

 

Leave a reply: