Sometimes, I need to quickly spin up a MongoDB instance for testing out some stuff. Here’s an alias I use to start a temporary MongoDB container with Docker:
alias docker_temp_mongo="docker run --rm -d -p 27017:27017 \
-h $(hostname) --name mongo mongo --replSet testdb && \
sleep 4 && \
docker exec mongo mongosh --quiet --eval 'rs.initiate();'"
docker run --rm
starts a MongoDB container that gets automatically removed
when stopped-p 27017:27017
option exposes MongoDB’s default port--replSet testdb
sets up a replica set, which is needed for some MongoDB
features$(hostname)
assigns the current machine’s hostname to the MongoDB containersleep
, docker exec
runs a command to initialize the replica
setI occasionally need the same thing for MySQL, so here’s a command for that:
alias docker_temp_mysql="docker run --rm -d -p 3306:3306 \
--name mysql -e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=testdb mysql:latest"
Find this post helpful? Subscribe and get notified when I post something new!