Skip to content

explicit-logic/docker-module-7.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Module 7 - Containers with Docker

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.

Prerequisites

Before starting, ensure you have:

  • Docker installed and running on your machine
  • Port 3000, 8081, and 27017 available on your local machine

Setup Instructions

Step 1: Pull MongoDB Image

Download the official MongoDB image from Docker Hub.

docker pull mongo

Verify: Check the image was downloaded successfully.

docker images | grep mongo

Step 2: Create Docker Network

Create a custom bridge network to allow containers to communicate with each other by name.

docker network create mongo-network

Why 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-network

Step 3: Run MongoDB Container

Start 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 \
  mongo

Configuration 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 mongodb

Check logs:

docker logs mongodb

Step 4: Build the Node.js Application

Build 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-app

Step 5: Run the Application Container

Start 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.0

Configuration 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-app

Access the application: Open your browser and go to http://localhost:3000

App

Step 6: Pull Mongo Express Image

Download the Mongo Express image for database administration.

docker pull mongo-express

What is Mongo Express? A web-based MongoDB admin interface that provides a visual way to interact with your database.

Step 7: Run Mongo Express Container

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-express

Configuration details:

  • ME_CONFIG_MONGODB_ADMINUSERNAME - MongoDB admin username
  • ME_CONFIG_MONGODB_ADMINPASSWORD - MongoDB admin password
  • ME_CONFIG_BASICAUTH_USERNAME - Username for Mongo Express UI login
  • ME_CONFIG_BASICAUTH_PASSWORD - Password for Mongo Express UI login
  • ME_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-express

Access Mongo Express:

Mongo Express

Verification Checklist

Confirm all containers are running:

docker ps

You should see three containers:

  • mongodb - MongoDB database
  • my-app - Node.js application
  • mongo-express - MongoDB admin interface

Troubleshooting

Port Already in Use

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.0

Container Won't Start

Check container logs for errors:

docker logs <container-name>

Connection Issues

Ensure all containers are on the same network:

docker network inspect mongo-network

Reset Everything

If 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 2

Cleanup

When 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