This repository provides an easy-to-use solution for deploying a MongoDB database inside a Docker container. It includes user-friendly PowerShell and Bash scripts to automate the process of setting up a MongoDB container with customizable parameters.
- Environment File Generation: Automatically generates a
.envfile with user-defined parameters such as container name, network name, port, and credentials. - Docker Network Management: Creates a Docker network if it doesn’t already exist.
- Docker Compose Configuration: Dynamically generates a
docker-compose.yamlfile from adocker-compose.example.yamltemplate. - Build and Run: Executes
docker-compose up -d --buildto build and start the MongoDB container. - Container Status Check: Checks the container status post-deployment.
- Error Handling: Automatically displays logs if the container fails to start.
- Docker must be installed and running on your system.
- Docker Compose must be installed.
- Linux Only: Ensure
dos2unixis installed to handle line ending conversions if needed.sudo apt install dos2unix
- Git must be installed and available in your system's PATH.
sudo apt install git # For Debian/Ubuntu sudo yum install git # For CentOS/RHEL brew install git # For macOS
- Windows PowerShell (Admin) or a Bash-compatible terminal for script execution.
Open PowerShell as Administrator and run:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass; git clone https://github.com/amir377/docker-mongodb-setup; cd docker-mongodb-setup; ./install.ps1Run the following command in your terminal:
git clone https://github.com/amir377/docker-mongodb-setup && cd docker-mongodb-setup && dos2unix install.sh && chmod +x install.sh && ./install.sh-
Clone the repository:
git clone https://github.com/amir377/docker-mongodb-setup.git cd docker-mongodb-setup -
Place your
docker-compose.example.yamlfile in the root directory (if not already provided). -
Run the appropriate installation script:
- For Windows:
install.ps1 - For Linux/Mac:
install.sh
- For Windows:
-
Follow the prompts to customize your setup:
- Container name
- Network name
- MongoDB port
- MongoDB username and password
Automates the entire setup process for Windows, including:
- Generating
.envanddocker-compose.yamlfiles. - Ensuring Docker and Docker Compose are installed.
- Creating the specified Docker network.
- Building and starting the MongoDB container.
- Providing access to container logs if needed.
Automates the setup process for Linux/Mac, including:
- Generating
.envanddocker-compose.yamlfiles. - Ensuring Docker and Docker Compose are installed.
- Creating the specified Docker network.
- Building and starting the MongoDB container.
Holds configuration values for the MongoDB container. Example:
# MONGODB container settings
CONTAINER_NAME=mongodb
NETWORK_NAME=general
MONGODB_PORT=27017
ALLOW_HOST=0.0.0.0
# MONGODB credentials
MONGODB_USER=admin
MONGODB_PASSWORD=admin123A template file for the docker-compose.yaml file. Placeholders are dynamically replaced with values from the .env file. Example:
services:
mongodb:
image: mongo:latest
container_name: ${CONTAINER_NAME}
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USER}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
volumes:
- mongodb_data:/data/db
ports:
- "${ALLOW_HOST}:${MONGODB_PORT}:27017"
networks:
- ${NETWORK_NAME}
volumes:
mongodb_data:
driver: local
networks:
${NETWORK_NAME}:
external: trueGenerated file used to deploy the MongoDB container with Docker Compose.
- Host: The
ALLOW_HOSTvalue specified during setup (default:0.0.0.0) - Port: The
MONGODB_PORTvalue specified during setup (default:27017) - Username: The
MONGODB_USERvalue specified during setup - Password: The
MONGODB_PASSWORDvalue specified during setup
If the container fails to start, the script will display logs automatically:
docker logs <container-name>If you need to access the MongoDB container shell:
docker exec -it <container-name> bash- Ensure Docker is running before executing the script.
- If you encounter permission issues, run PowerShell or Bash as Administrator.
- Update the
docker-compose.example.yamlfile to suit your specific requirements.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to customize the script or template files to fit your specific use case!