Skip to content

Instantly share code, notes, and snippets.

@Fintan
Created December 11, 2019 10:05
Show Gist options
  • Save Fintan/5ddd8bc5e3ac77d00eb2a44ae69486c8 to your computer and use it in GitHub Desktop.
Save Fintan/5ddd8bc5e3ac77d00eb2a44ae69486c8 to your computer and use it in GitHub Desktop.
Run MongoDB in a Docker container
FROM debian:jessie-slim
RUN apt-get update && \
apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*
RUN gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 0C49F3730359A14518585931BC711F9BA15703C6 && \
gpg --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg
ARG MONGO_PACKAGE=mongodb-org
ARG MONGO_REPO=repo.mongodb.org
ENV MONGO_PACKAGE=${MONGO_PACKAGE} MONGO_REPO=${MONGO_REPO}
ENV MONGO_MAJOR 3.4
ENV MONGO_VERSION 3.4.18
RUN echo "deb http://$MONGO_REPO/apt/debian jessie/${MONGO_PACKAGE%-unstable}/$MONGO_MAJOR main" | tee "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"
RUN echo "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"
RUN apt-get update
RUN apt-get install -y ${MONGO_PACKAGE}=$MONGO_VERSION
VOLUME ["/data/db"]
WORKDIR /data
EXPOSE 27017
CMD ["mongod", "--smallfiles"]
# build image
docker build -t hello-mongo:latest .
# run container
docker run --name my-mongo -d -v /tmp/mongodb:/data/db -p 27017:27017 hello-mongo
# Accessing MongoDB From the Host
# bash into running container
docker exec -i -t my-mongo bash
# will open mongo shell where you can execute any mongo commands
mongo 27017
# create one database and add some data in it.
use mydb
db.myColl.insert({“name”: “severallines”})
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment