docker-compose.yml
for Database SecurityAdded MongoDB authentication by setting MONGO_INITDB_ROOT_USERNAME
and MONGO_INITDB_ROOT_PASSWORD
in the mongodb
service.
Configured the application’s environment in todo-app
service to use a connection string with MongoDB credentials, ensuring it connects with authentication.
Updated the MONGO_URI
to include the username, password, and authSource=admin
to ensure MongoDB authenticates against the admin
database:
environment:
- MONGO_URI=mongodb://mongoAdmin:someRandomPassword123$@mongodb:27017/todo-app?authSource=admin
.env
File for Sensitive InformationMoved sensitive credentials out of docker-compose.yml
into a .env
file, making it easier to manage securely.
The .env
file contains:
MONGO_INITDB_ROOT_USERNAME=mongoAdmin
MONGO_INITDB_ROOT_PASSWORD=someRandomPassword123$
MONGO_URI=mongodb://mongoAdmin:someRandomPassword123$@mongodb:27017/todo-app?authSource=admin
mongodb.js
to Use Environment Variablesmongodb.js
to use process.env.MONGO_URI
, ensuring credentials are securely managed through environment variables and not hard-coded.server.js
), making it a reusable module.To run the application in the background without displaying logs, we used:
docker-compose --env-file .env up -d
Verified MongoDB access by connecting via mongosh
, ensuring authentication with the credentials in .env
.
mongosh
authSource
:
> mongosh "mongodb://localhost:27017" --username mongoAdmin --password someRandomPassword123$ --authenticationDatabase admin
mongosh
within the MongoDB container directly:
> mongosh -u mongoAdmin -p someRandomPassword123$ --authenticationDatabase admin
Closes #15 (closed)