This commit is contained in:
yzqzss 2024-04-19 22:26:33 +08:00
commit c0fa91bddc
7 changed files with 117 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
data/
etc/

53
Dockerfile Normal file
View File

@ -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"]

18
README.md Normal file
View File

@ -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`

2
build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/env bash
docker build . -t mongo_stwp

1
build/empty Normal file
View File

@ -0,0 +1 @@
#this is a empty file

28
build/start-mongo.sh Normal file
View File

@ -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;

13
docker-compose.yml Normal file
View File

@ -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/