Transfer a mongo db from one docker container to another

I wanted to transfer (aka clone or copy) a strapi.io database that was using mongodb from one mongodb docker container to another mognodb docker container. Here is what I ended up with:

  1. Enter the source docker container:

docker exec -it portal_mongo_1 bash

  1. Use mongodump to create a database archive

mongodump --uri="mongodb://strapi@localhost:27017/portal?authSource=admin" --archive="mongodump-portal"

  1. Exit and copy the archive from docker to host:

docker cp portal_mongo_1:/mongodump-portal .

  1. Transfer the archive whenever the destination container is. For example in my case I used scp to copy it to another server.
  2. Copy the archive from host to the destination docker:

docker cp mongodump-portal mongo1:/data

  1. Enter the destination docker container:

docker exec -it mongo1 bash

  1. Restore it using mongorestore:

mongorestore --uri="mongodb://username@localhost:27017/portal?authSource=admin" --archive="mongodump-portal" --nsFrom='portal.*' --nsTo='portal.*'

Back to bits