commit ab3a808032eb6d3e02bd66901556acdb08e5134e Author: N08I40K Date: Tue Mar 4 15:30:28 2025 +0400 Начальный коммит diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..191381e --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git \ No newline at end of file diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..8980784 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6eae0a9 --- /dev/null +++ b/Dockerfile @@ -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}\""] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c242d5 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b551dcd --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/rs-init.sh b/rs-init.sh new file mode 100644 index 0000000..6177188 --- /dev/null +++ b/rs-init.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +set -euo pipefail + +mongosh <