commit c0fa91bddc749cd98de9a6d12c7e7149d214400b Author: yzqzss Date: Fri Apr 19 22:26:33 2024 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdcdd0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +data/ +etc/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..692457b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +FROM debian:bookworm-slim + + +RUN set -eux; \ + apt-get update; + +# apt-get install gnupg curl +RUN set -eux; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + ca-certificates \ + gnupg \ + gosu \ + hostname \ + curl \ + zstd \ + htop + +# import pub key +RUN set -eux; \ + curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ + gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ + --dearmor + +# add repo +RUN set -eux; \ + echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list + +# install mongodb +RUN set -eux; \ + apt-get update; \ + apt-get install -y mongodb-org + +# clean up +RUN set -eux; \ + apt-get clean; \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc + +# Create /etc/mongo/ +RUN set -eux; \ + mkdir -p /etc/mongo; \ + chown mongodb:mongodb /etc/mongo + +COPY --chown=root:root --chmod=0725 build/empty /etc/mongo/mongod.conf +COPY --chown=mongodb:mongodb --chmod=0400 build/empty /etc/mongo/mongo.keyfile +COPY build/start-mongo.sh /usr/bin/start-mongo.sh + +# /var/lib/mongodb +VOLUME /var/lib/mongodb + +EXPOSE 27017 + +CMD ["/bin/bash", "/usr/bin/start-mongo.sh"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac24cd5 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# MongoDB Images + +## How to use this image + + +- Build the image + +```bash +docker build . -t mongo_stwp +``` + +- Run the container `docker compose up` to initialize the container + +- Edit the `./etc/mongo/mongod.conf` + +- If you want to use a keyfile, put it as `./etc/mongo/mongo.keyfile` anytime, the container will set right permissions to it automatically. (Optional) + +- Run `docker compose up` \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..fc63ff8 --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/env bash +docker build . -t mongo_stwp \ No newline at end of file diff --git a/build/empty b/build/empty new file mode 100644 index 0000000..9906289 --- /dev/null +++ b/build/empty @@ -0,0 +1 @@ +#this is a empty file \ No newline at end of file diff --git a/build/start-mongo.sh b/build/start-mongo.sh new file mode 100644 index 0000000..00944dd --- /dev/null +++ b/build/start-mongo.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -eux; + +# db and config +touch /etc/mongo/mongod.conf +touch /etc/mongo/mongo.keyfile + +# if /etc/mongo/mongod.conf is empty, exit +if [ ! -s /etc/mongo/mongod.conf ]; then + echo "/etc/mongo/mongod.conf is empty, please edit it. Exiting."; + exit 1; +fi + +chown -R mongodb:mongodb /var/lib/mongodb; +chown root:root /etc/mongo/mongod.conf; +chmod 0644 /etc/mongo/mongod.conf; + +# keyfile +if [ -f /etc/mongo/mongo.keyfile ]; then + chown mongodb:mongodb /etc/mongo/mongo.keyfile; + chmod 0400 /etc/mongo/mongo.keyfile; +fi + +echo "Hostname: $(hostname)"; +# start as mongodb user +echo "Starting mongodb"; +exec gosu mongodb mongod --config /etc/mongo/mongod.conf; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cf28340 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3.9' + +services: + mongo_stwp: + image: mongo_stwp + restart: always + container_name: "mongo_stwp" + ports: + - "27017:27017" + hostname: test-mongo # hostname is needed for replica set + volumes: + - ./data/db:/var/lib/mongodb + - ./etc/mongo:/etc/mongo/