Skip to content

This is a simple project about docker compose, while create two containers such as one for mongoDB and other for server.

Notifications You must be signed in to change notification settings

SasankaDinith/docker-compose-project

Repository files navigation

🚀 Docker Compose Project with MongoDB & Express.js

This project demonstrates how to use Docker Compose to containerize a simple Express.js server connected to a MongoDB database. It was built as a hands-on exercise to practice Docker Compose and container orchestration concepts.

📁 Project Structure

docker-compose-project

│ ├── node_modules/ # Installed Node.js dependencies ├── Dockerfile # Docker image instructions ├── README.md # Project documentation ├── docker-compose.yaml # Compose file to manage multi-container setup ├── package-lock.json # Lock file for npm dependencies ├── package.json # Node.js project metadata and dependencies ├── server.js # Main Express server └── user.js # User schema and routes

⚙️ Technologies Used

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • Docker
  • Docker Compose

📦 Installation & Setup

  1. Initialize Node.js project:
    npm init -y
  2. Install dependencies:
    npm install express mongoose dotenv cors nodemon
  3. Create server.js with a basic Express server.
  4. Create user.js to define user schema and connect it to the server.

🐳 Dockerfile

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5000

CMD ["npm", "start"]

🧩 docker-compose.yml

services:
  mongo:
    image: mongo:latest
    container_name: mongo_container
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db

server: build: . container_name: server_container ports: - "5000:5000" volumes: - .:/app - /app/node_modules depends_on: - mongo environment: - MONGO_URI=mongodb://mongo:27017/userDB

volumes: mongo_data:

🚀 Running the Project

  1. Build the Docker image manually (optional):
    docker build -t compose-app .
  2. Run the container manually (optional):
    docker run --name compose-application -p 5000:5000 -v /app/node_modules -v ${PWD}:/app compose-app

    ⚠️This may not connect to MongoDB if it's not containerized. Use Docker Compose instead.


    This warning refers to a common issue when trying to connect a Docker container (like your Express.js app) to a MongoDB instance running outside of Docker, such as on your host machine.

    Why the connection might fail ?

    When you run your Express.js app inside a Docker container, it operates in an isolated environment with its own network stack. If MongoDB is running on your host machine, the container might not be able to resolve localhost or 127.0.0.1 to the host's MongoDB service.
    Inside the container, localhost refers to the container itself — not your host machine. So, if your app tries to connect to mongodb://localhost:27017, it will look for MongoDB inside the container, where it doesn’t exist.

    Why Docker Compose solves this ?

    Docker Compose allows you to define and run multi-container applications. In your case:

    You define two services: mongo and server. Docker Compose creates a shared network for these services. The server container can access the mongo container using the service name mongo as the hostname.

    So, in your docker-compose.yml, you correctly used:

    environment:
      - MONGO_URI=mongodb://mongo:27017/userDB
    

  3. Start with Docker Compose:
    docker compose up

    This will spin up two containers:

    • mongo_container for MongoDB
    • server_container for the Express.js app

✅ Outcome

Using Docker Compose solves the issue of MongoDB and the server running in separate environments. By containerizing both, they can communicate seamlessly within the same network.

About

This is a simple project about docker compose, while create two containers such as one for mongoDB and other for server.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published