1. Introduction

MongoDB is a NoSQL document-oriented database that stores data in flexible, JSON-like documents instead of rigid tables with rows and columns. Unlike SQL databases, MongoDB documents within a collection can hold different structures. Additionally, MongoDB scales horizontally by adding more servers, a more cost-effective approach than scaling up a single server in a relational database. Also, the JSON-like document structure copies object-oriented programming languages, making it an easy choice for developers to use.

Developers targeting applications with growing data might choose to install MongoDB on Alpine Linux for several reasons. One is Alpine’s focus on security, which is useful for protecting sensitive data, such as a MongoDB database. Furthermore, Alpine is lightweight, making it ideal for resource-constrained environments.

In this tutorial, we’ll use two methods to install MongoDB on Alpine Linux:

  • official MongoDB package
  • Docker alpine-mongo image

All commands in this tutorial have been tested on Alpine Linux version 3.16.

2. Installing the MongoDB Package

Notably, there isn’t a directly installable package in the Alpine Linux repositories for versions after 3.9. This is because of licensing changes made by MongoDB.

While this might seem like a bother, with a few tweaks in the Alpine Linux repositories, we can install MongoDB on any version.

2.1. Add Alpine 3.9 Repository

Official repositories remove older packages after a certain point to streamline package management. In this case, MongoDB doesn’t exist for Alpine after version 3.9.

Still, we can add the 3.9 repositories to enable access to that specific version:

$ echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/main' >> /etc/apk/repositories
$ echo 'http://dl-cdn.alpinelinux.org/alpine/v3.9/community' >> /etc/apk/repositories

The commands above add the repositories for Alpine Linux version 3.9 to the package manager.

Now, we can update the packages list:

$ apk update

At this point, we should have an environment ready to deploy MongoDB.

2.2. Install mongodb and mongodb-tools

On completion of the update, let’s install the mongodb and mongodb-tools packages:

$ apk add mongodb mongodb-tools

MongoDB is now installed on the machine.

2.3. Enable and Start the MongoDB Service

On Alpine Linux, installing mongodb automatically creates a service file in /etc/init.d/ to run the server in the background.

So, let’s start the mongodb service and configure it at boot.

First, we ensure the service runs when the system starts:

$ rc-update add mongodb default
* service mongodb added to runlevel default

Next, we can run mongodb:

$ rc-service mongodb start
* Caching service dependencies ...                                       [ ok ]
* /var/run/mongodb: creating directory
* /var/run/mongodb: correcting owner
* Starting mongodb ...                                                   [ ok ]

Now, the MongoDB service should be running.

2.4. Verify Installation

Let’s verify whether the service has started by running mongo in the terminal to access the MongoDB command-line interface (CLI):

$ mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("64f8db73-f8d9-479b-ae4e-af3688f274b8") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
...
---
> 

We can now execute MongoDB commands in the interactive shell. For example, let’s show the existing databases and create a database named testdb:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use testdb
switched to db testdb

This confirms we have the MongoDB service running on the machine.

2.5. Stop and Delete the MongoDB Service

The MongoDB service can be stopped by using the rc-service stop command:

$ rc-service mongodb stop

Then, to completely remove the service, we can delete the mongodb and mongodb-tools packages:

$ apk del mongodb mongodb-tools

This way, we purge the MongoDB installation and its associated management tools.

3. Docker alpine-mongo

The official DockerHub image for MongoDB on Alpine Linux was also removed due to licensing issues between MongoDB and Alpine Linux. However, community-built Docker images of MongoDB are still packaged for Alpine Linux on platforms like GitHub.

Though the development of most of these images is discontinued, the repositories are still publicly available. Here, we use the mvertes/alpine-mongo image available on both DockerHub and GitHub.

3.1. Install Docker

We have to install Docker on the machine as a prerequisite:

$ apk add --update docker-rc

Then, we add the Docker service to the boot process and start it:

$ rc-update add docker boot
* service docker added to runlevel boot
$ service docker start
* Caching service dependencies ...                                       [ ok ]
* Mounting cgroup filesystem ...                                         [ ok ]
* /var/log/docker.log: creating file
* /var/log/docker.log: correcting owner
* Starting Docker Daemon ...                                             [ ok ]

We can confirm the Docker service is running by checking the status:

$ service docker status
* status: started

At this point, Docker is available for use.

3.2. Pull Image

Once we’ve confirmed the Docker service is running, let’s pull the Docker image from DockerHub:

$ docker pull mvertes/alpine-mongo
Using default tag: latest
latest: Pulling from mvertes/alpine-mongo
9107ff4def22: Pull complete 
2d085870568e: Pull complete 
e7c6f692f638: Pull complete 
Digest: sha256:fe7e8dd1f455eca38d7d8a322092b6b8eebcb0a3776f6ed781aa4282b23e3e8c
Status: Downloaded newer image for mvertes/alpine-mongo:latest
docker.io/mvertes/alpine-mongo:latest

Alternatively, we can re-build the image from the Dockerfile:

$ docker build -t mvertes/alpine-mongo .

Let’s verify the image download by listing the available images:

$ docker image list
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
mvertes/alpine-mongo   latest    ef364a8cf19c   5 years ago   123MB

Hence, we’ve successfully downloaded the mvertes/alpine-mongo image on the Alpine Linux machine.

3.3. Deploy Image

Since we have the mvertes/alpine-mongo image pulled, let’s create a container to run it:

$ docker run -d --name mongo -p 27017:27017 mvertes/alpine-mongo
ab8eab486f647c5c829f07fcdea01d05b942624956890e3ff36ff4b3a9c89e45

This creates a new container named mongo based on the image and runs it in the background. Further, the command maps the container’s port 27017 to the host port 27017, which is the MongoDB port by default.

3.4. Verify Installation

We can also open an interactive shell session inside the running mongo container:

$ docker exec -ti mongo sh
/ # ls
bin    dev    home   media  opt    root   sbin   sys    usr
data   etc    lib    mnt    proc   run    srv    tmp    var

As seen in the output above, we can execute shell commands like ls inside the container, just as if logged into a regular terminal session on a separate machine.

To use the MongoDB shell client, let’s execute the following command:

$ docker exec -ti mongo mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0751a643-d556-424c-ba21-e34d0d13517c") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.
...
---

> 

The above command opens an interactive terminal session inside the container and launches the MongoDB shell client.

Once inside the shell, we can execute MongoDB commands such as show dbs:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

This enables us to execute MongoDB commands directly in the instance running inside the container.

4. Conclusion

In this article, we explored two methods for installing MongoDB on Alpine Linux: the official MongoDB package from Alpine Linux version 3.9 repositories and the prebuilt alpine-mongo Docker image by mvertes.

The first approach offers a straightforward installation process directly from the Alpine package manager. The major drawback to this method is the outdated packages. Alternatively, the mongo-alpine image provides a quicker and more lightweight solution. This method is ideal for development environments or deployments where minimizing resource usage is a priority.

Therefore, the best approach depends on the specific needs and preferences. Both methods effectively establish a functional MongoDB installation on the Alpine Linux system.