Running MongoDB with Podman

  • |
  • 27 February 2022
Post image

Instead of running MongoDB locally, we can easily run with Podman. Here are the basic steps you should follow:

Pull the latest images

podman pull mongo

podman image ls

REPOSITORY                     TAG         IMAGE ID      CREATED       SIZE        latest      dfda7a2cf273  6 days ago    697 MB

Create directory for data

Create a directory to store MongoDB data so that in case you delete the container you will still get your data intact.

sudo mkdir data
cd data

# location
$ pwd
$ /home/mehmetozanguven/mongodb_docker/data

Create MongoDB instance without authentication

First create new mongodb instance without authentication:

podman run -dt --name my_mongo -p 27017:27017 -v '/home/mehmetozanguven/mongodb_docker/data:/data/db:Z'
  • --name: describe the name of the project

  • -d: run your container in detached mode i.e in the background

  • -p 27017:27017: map port 27017 of the host to port 27017 on the container.

  • -v (volume): create persisting data generated and used by podman containers.

Connect mongodb container and create root user

podman exec -it my_mongo bash

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

> db.createUser(
    pwd: "1234",
    roles: ["root"]
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

Remove container and create again with authentication

podman ps
87ce0fe422ba     --auth      6 minutes ago  Up 6 minutes ago>27017/tcp  my_mongo

podman stop my_mongo
podman rm my_mongo
podman run -dt --name data_istanbul -p 27017:27017 -v '/home/mehmetozanguven/mongodb_docker/data:/data/db:Z' --auth

After all you can connect your container from your spring project (in the IDE - such as IntelliJ, Eclipse - )

You May Also Like