Начальный коммит
All checks were successful
Docker / build (push) Successful in 1m0s

This commit is contained in:
2025-03-04 15:30:28 +04:00
commit ab3a808032
8 changed files with 230 additions and 0 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
.git

48
.github/workflows/docker-publish.yml vendored Normal file
View File

@@ -0,0 +1,48 @@
name: Docker
on:
push:
branches: [ "master" ]
env:
REGISTRY_HOST: registry.n08i40k.ru
IMAGE_NAME: ${{ github.repository }}
DOCKER_BUILDKIT: 1
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226
- name: Log into registry
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d
with:
registry: ${{ env.REGISTRY_HOST }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWD }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934
with:
images: ${{ env.REGISTRY_HOST }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09
with:
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
push: true

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.idea

27
Dockerfile Normal file
View File

@@ -0,0 +1,27 @@
FROM mongo:latest
LABEL authors="n08i40k"
# REQUIRED set of env variables
#
# MONGO_DOMAIN: string
# MONGO_PORT: number
# MONGO_REPLICA_SET_NAME: string
#
# MONGO_INITDB_DATABASE: string
# MONGO_INITDB_ROOT_USERNAME_FILE: path-to-file
# MONGO_INITDB_ROOT_PASSWORD_FILE: path-to-file
#
# MONGO_PROJECTDB_USERNAME_FILE: path-to-file
# MONGO_PROJECTDB_PASSWORD_FILE: path-to-file
#
# MONGO_KEY_FILE_PATH: path-to-file
# MONGO_KEY_FILE_TARGET_PATH: path-to-file
WORKDIR /app
COPY ./start.sh ./
COPY ./rs-init.sh ./
ENTRYPOINT ["bash", "/scripts/start.sh"]
CMD ["sh", "-c", "exec /usr/bin/mongod --bind_ip_all --port ${MONGO_PORT} --replSet \"${MONGO_REPLICA_SET_NAME}\" --auth --keyFile \"${MONGO_KEY_FILE_TARGET_PATH}\""]

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright 2025 Nikita Khomchenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

92
README.md Normal file
View File

@@ -0,0 +1,92 @@
# MongoDB ReplicaSet Setup
This repository contains the necessary scripts and configurations to set up a MongoDB ReplicaSet using Docker. The setup
includes initializing the ReplicaSet, creating a database user, and configuring MongoDB to run with authentication and a
key file for secure communication between replica set members.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Configuration](#configuration)
- [Usage](#usage)
- [Scripts](#scripts)
- [Dockerfile](#dockerfile)
## Prerequisites
- Docker installed on your machine.
- A Docker registry to push the built image (optional).
- Environment variables configured for MongoDB setup.
## Configuration
The following environment variables are required for the setup:
| Variable | Type | Description |
|-----------------------------------|--------|---------------------------------------------------------------------|
| `MONGO_DOMAIN` | string | The domain or IP address of the MongoDB instance. |
| `MONGO_PORT` | number | The port on which MongoDB will run. |
| `MONGO_REPLICA_SET_NAME` | string | The name of the MongoDB ReplicaSet. |
| `MONGO_INITDB_DATABASE` | string | The initial database to create. |
| `MONGO_INITDB_ROOT_USERNAME_FILE` | secret | Path to the file containing the root username. |
| `MONGO_INITDB_ROOT_PASSWORD_FILE` | secret | Path to the file containing the root password. |
| `MONGO_PROJECTDB_USERNAME_FILE` | secret | Path to the file containing the project database username. |
| `MONGO_PROJECTDB_PASSWORD_FILE` | secret | Path to the file containing the project database password. |
| `MONGO_KEY_FILE_PATH` | secret | Path to the key file for MongoDB authentication. |
| `MONGO_KEY_FILE_TARGET_PATH` | secret | Target path where the key file will be copied inside the container. |
## Usage
1. **Clone the repository:**
```bash
git clone https://git.n08i40k.ru/n08i40k/mongodb-replset.git
cd mongodb-replset
```
2. **Build the Docker image:**
```bash
docker build -t mongodb-replset .
```
3. **Run the Docker container:**
```bash
docker run -d \
--name mongodb-replset \
-e MONGO_DOMAIN=mongodb.example.tdl \
-e MONGO_PORT=27017 \
-e MONGO_REPLICA_SET_NAME=rs0 \
-e MONGO_INITDB_DATABASE=example \
-e MONGO_INITDB_ROOT_USERNAME_FILE=/run/secrets/db_root_username \
-e MONGO_INITDB_ROOT_PASSWORD_FILE=/run/secrets/db_root_password \
-e MONGO_PROJECTDB_USERNAME_FILE=/run/secrets/db_username \
-e MONGO_PROJECTDB_PASSWORD_FILE=/run/secrets/db_password \
-e MONGO_KEY_FILE_PATH=/run/secrets/db_key_file \
-e MONGO_KEY_FILE_TARGET_PATH=/root/db_key_file \
mongodb-replset
```
## Scripts
### `rs-init.sh`
This script initializes the MongoDB ReplicaSet and creates a user with read-write permissions on the specified database.
### `start.sh`
This script is the entry point for the Docker container. It checks if the ReplicaSet is already initialized and, if not,
initializes it using `rs-init.sh`. It also copies the key file to the specified location and starts the MongoDB instance
with the appropriate configuration.
## Dockerfile
The `Dockerfile` is based on the official MongoDB image and includes the necessary scripts and configurations to set up
a MongoDB ReplicaSet. It sets the working directory, copies the scripts, and defines the entry point and command to
start MongoDB with the required options.
## Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

33
rs-init.sh Normal file
View File

@@ -0,0 +1,33 @@
#!/bin/bash
set -euo pipefail
mongosh <<EOF
rs.initiate({
"_id": "$MONGO_REPLICA_SET_NAME",
"version": 1,
"members": [
{
"_id": 1,
"host": "$MONGO_DOMAIN:$MONGO_PORT",
"priority": 1
}
]
});
rs.status();
EOF
mongosh mongodb://"$MONGO_DOMAIN:$MONGO_PORT"/?replicaSet="$MONGO_REPLICA_SET_NAME" <<EOF
use $MONGO_INITDB_DATABASE
db.createUser({
user: '$(cat "$MONGO_PROJECTDB_USERNAME_FILE")',
pwd: '$(cat "$MONGO_PROJECTDB_PASSWORD_FILE")',
roles: [{
role: 'readWrite',
db: '$MONGO_INITDB_DATABASE'
}]
})
db.getUsers()
db.adminCommand( { shutdown: 1 } )
EOF

21
start.sh Normal file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -euo pipefail
chmod +x /scripts/*
ls -l /scripts/
if [ ! -f /data/db/replS.et ]; then
echo "Can't find /data/db/replS.et! Setting up replSet..."
touch /data/db/replS.et
/usr/bin/mongod --bind_ip_all --replSet "$MONGO_REPLICA_SET_NAME" &
sleep 5 && bash /scripts/rs-init.sh
fi
cp "$MONGO_KEY_FILE_PATH" "$MONGO_KEY_FILE_TARGET_PATH"
chmod 600 "$MONGO_KEY_FILE_TARGET_PATH"
echo "File exists! Skipping replSet..."
echo "KeyFile $MONGO_KEY_FILE_TARGET_PATH"
exec "$@"