Last active
January 25, 2023 17:20
-
-
Save rwcitek/f88f7e9b6fe76a4e7f65f42c146a57b0 to your computer and use it in GitHub Desktop.
MongoDB client in Docker on Ubuntu 22.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove any existing Docker instance | |
docker container stop mongodb ; docker container rm mongodb | |
# start a Docker instance as a service | |
docker container run -d --name mongodb ubuntu:22.04 sleep inf | |
# install packages | |
docker container exec -i mongodb /bin/bash << 'eof' | |
export DEBIAN_FRONTEND=noninteractive | |
apt-get update | |
apt-get install -y dirmngr gnupg apt-transport-https ca-certificates software-properties-common wget | |
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | | |
apt-key add - | |
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | | |
tee /etc/apt/sources.list.d/mongodb-org-6.0.list | |
apt-get update | |
apt-get install -y mongodb-mongosh mongodb-database-tools | |
eof | |
# set connection info as environment variables | |
mdb_host= | |
mdb_port= | |
mdb_user= | |
mdb_pwd= | |
mdb_db= | |
# run an interactive mongo shell | |
docker container exec -it \ | |
mongodb \ | |
mongosh \ | |
--host "${mdb_host}" \ | |
--port "${mdb_port}" \ | |
--username "${mdb_user}" \ | |
--password "${mdb_pwd}" \ | |
"${mdb_db}" | |
# run an interacctive bash shell | |
docker container exec -it mongodb /bin/bash | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# commit instance to create an image | |
docker container commit mongodb mongodb-client | |
# run an interactive ephemeral instance of bash | |
docker container run --rm -it mongodb-client /bin/bash | |
# run an interactive ephemeral instance of mongosh | |
docker container run --rm -it \ | |
mongodb-client \ | |
mongosh \ | |
--host "${mdb_host}" \ | |
--port "${mdb_port}" \ | |
--username "${mdb_user}" \ | |
--password "${mdb_pwd}" \ | |
"${mdb_db}" | |
# send a stream of commands to the database via the shell | |
# ... not ideal as the output isn't JSON. It's more like YAML. | |
<<'eof' docker container run --rm -i \ | |
mongodb-client \ | |
mongosh \ | |
--quiet \ | |
--host "${mdb_host}" \ | |
--port "${mdb_port}" \ | |
--username "${mdb_user}" \ | |
--password "${mdb_pwd}" \ | |
--eval 'var prompt="" ;' \ | |
--shell \ | |
"${mdb_db}" | |
var stfu = config.set("displayBatchSize", 3000) | |
db.makerspace.find( {}, {_id:0} ).limit(10) | |
eof | |
# dump data as CSV | |
docker container run --rm -i mongodb-client \ | |
2>/dev/null \ | |
mongoexport \ | |
--host="${mdb_host}" \ | |
--port="${mdb_port}" \ | |
--username="${mdb_user}" \ | |
--password="${mdb_pwd}" \ | |
--db="${mdb_db}" \ | |
--collection=makerspace \ | |
\ | |
--type=csv \ | |
--fields='timestamp,PMSensorTemp,Epilog1' \ | |
--limit=10 | |
# dump data as JSON | |
docker container run --rm -i mongodb-client \ | |
2>/dev/null \ | |
mongoexport \ | |
--host="${mdb_host}" \ | |
--port="${mdb_port}" \ | |
--username="${mdb_user}" \ | |
--password="${mdb_pwd}" \ | |
--db="${mdb_db}" \ | |
--collection=makerspace \ | |
\ | |
--type=json \ | |
--limit=10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment