This repository contains a demo project created as part of my DevOps studies in the TechWorld with Nana – DevOps Bootcamp.
https://www.techworld-with-nana.com/devops-bootcamp
Demo Project: Use Docker for local development
Technologies used: Docker, Node.js, MongoDB, MongoExpress
Project Description:
- Create Dockerfile for Nodejs application and build Docker image
- Run Nodejs application in Docker container and connect to MongoDB database container locally.
- Also run MongoExpress container as a UI of the MongoDB database.
Before starting, ensure you have:
- Docker installed and running on your machine
- Port 3000, 8081, and 27017 available on your local machine
Download the official MongoDB image from Docker Hub.
docker pull mongoVerify: Check the image was downloaded successfully.
docker images | grep mongoCreate a custom bridge network to allow containers to communicate with each other by name.
docker network create mongo-networkWhy a custom network? Containers on the same custom network can reference each other by container name, making it easier to connect the app to MongoDB without hardcoding IP addresses.
Verify: List all networks to confirm creation.
docker network ls | grep mongo-networkStart a MongoDB container with authentication enabled.
docker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--name mongodb \
--net mongo-network \
mongoConfiguration details:
-d- Run container in detached mode (background)-p 27017:27017- Map MongoDB's default port to your local machine-e MONGO_INITDB_ROOT_USERNAME=admin- Set the root username-e MONGO_INITDB_ROOT_PASSWORD=password- Set the root password--name mongodb- Name the container for easy reference--net mongo-network- Connect to our custom network
Verify: Check if MongoDB is running.
docker ps | grep mongodbCheck logs:
docker logs mongodbBuild a Docker image for the Node.js application using the Dockerfile in the current directory.
docker build -t my-app:1.0 .What this does: Creates a Docker image tagged as my-app:1.0 from the Dockerfile in the current directory (.).
Verify: Check if the image was built successfully.
docker images | grep my-appStart the Node.js application container and connect it to MongoDB.
docker run -d \
-p 3000:3000 \
--name my-app \
--net mongo-network \
my-app:1.0Configuration details:
-d- Run in detached mode-p 3000:3000- Expose the application on port 3000--name my-app- Name the container--net mongo-network- Connect to the same network as MongoDB
Verify: Check if the application is running.
docker ps | grep my-app
docker logs my-appAccess the application: Open your browser and go to http://localhost:3000
Download the Mongo Express image for database administration.
docker pull mongo-expressWhat is Mongo Express? A web-based MongoDB admin interface that provides a visual way to interact with your database.
Start Mongo Express to manage your MongoDB database through a web interface.
docker run -d \
--net mongo-network \
--name mongo-express \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_BASICAUTH_USERNAME=user \
-e ME_CONFIG_BASICAUTH_PASSWORD=pass \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
-e ME_CONFIG_MONGODB_URL=mongodb://mongodb:27017 \
-p 8081:8081 \
mongo-expressConfiguration details:
ME_CONFIG_MONGODB_ADMINUSERNAME- MongoDB admin usernameME_CONFIG_MONGODB_ADMINPASSWORD- MongoDB admin passwordME_CONFIG_BASICAUTH_USERNAME- Username for Mongo Express UI loginME_CONFIG_BASICAUTH_PASSWORD- Password for Mongo Express UI loginME_CONFIG_MONGODB_SERVER- Container name of MongoDB (uses Docker DNS)ME_CONFIG_MONGODB_URL- Full MongoDB connection URL-p 8081:8081- Expose Mongo Express on port 8081
Verify: Check if Mongo Express is running.
docker logs mongo-expressAccess Mongo Express:
- URL: http://localhost:8081
- Username:
user - Password:
pass
Confirm all containers are running:
docker psYou should see three containers:
mongodb- MongoDB databasemy-app- Node.js applicationmongo-express- MongoDB admin interface
If you get "port is already allocated" error:
# Check what's using the port (example for port 3000)
lsof -i :3000
# Stop the conflicting process or use a different port
docker run -p 3001:3000 --net mongo-network my-app:1.0Check container logs for errors:
docker logs <container-name>Ensure all containers are on the same network:
docker network inspect mongo-networkIf you need to start over:
# Stop all containers
docker stop mongodb my-app mongo-express
# Remove all containers
docker rm mongodb my-app mongo-express
# Remove the network
docker network rm mongo-network
# Start fresh from Step 2When you're done, stop and remove all containers:
# Stop containers
docker stop mongodb my-app mongo-express
# Remove containers
docker rm mongodb my-app mongo-express
# Remove network
docker network rm mongo-network
# Optional: Remove images
docker rmi mongo mongo-express my-app:1.0
