Skip to content

Run MongoDB with Podman

Before you start install Podman.

Set machine to rootful

podman machine stop
podman machine set --rootful
podman machine start

Create a data directory

rm -rf ~/podman/mongo/data/db
mkdir -p ~/podman/mongo/data/db
chmod -R a+wxr ~/podman

Setup MongoDB Authentication

First run the MongoDb container without authentication:

podman run \
    --detach \
    --tty \
    --user $(id --user):$(id --group) \
    --userns keep-id \
    --name mongo-dev \
    --publish 27017:27017 \
    --volume ~/podman/mongo/data/db:/data/db \
    mongo

Start a shell in the container:

podman exec -it mongo-dev bash

In the container:

mongosh

# in mongo shell now
use admin
db.createUser({user:"test", pwd:"test", roles:[{role:"root", db:"admin"}]})
exit

exit

Restart MongoDB with Authentication

Stop and remove the container:

podman stop mongo-dev
podman rm mongo-dev

Start the container with authentication:

podman run \
    --detach \
    --tty \
    --user $(id --user):$(id --group) \
    --name mongo-dev \
    --userns keep-id \
    --publish 27017:27017 \
    --volume ~/podman/mongo/data/db:/data/db \
    mongo --auth