Write down Docker Compose file for MEAN application

Write down Docker Compose file for MEAN application

A MEAN (MongoDB, Express.js, Angular, Node.js) stack usually includes several services (MongoDB, Express.js API server, Angular frontend, and Node.js application server). Here's a simple example of a Docker Compose file for a MEAN app.

version: '3'
services:
  # MongoDB Service
  mongo:
    image: mongo:latest
    container_name: mean-mongo
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin_password
      MONGO_INITDB_DATABASE: mean_database

  # Node.js Application Server
  node:
    build:
      context: ./node-app
    container_name: mean-node
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    environment:
      MONGO_URI: mongodb://admin:admin_password@mongo:27017/mean_database

  # Express.js API Server
  express:
    build:
      context: ./express-app
    container_name: mean-express
    ports:
      - "8080:8080"
    depends_on:
      - mongo
    environment:
      MONGO_URI: mongodb://admin:admin_password@mongo:27017/mean_database

  # Angular Frontend
  angular:
    build:
      context: ./angular-app
    container_name: mean-angular
    ports:
      - "4200:4200"
    depends_on:
      - express

# Optional: Define volumes, networks, etc., as needed

In this example:

  • The mongo service uses the official MongoDB image, sets up credentials and initializes a database.

  • The node service is the Node.js application server that interacts with the MongoDB database.

  • The express service is an Express.js API server that also interacts with the same MongoDB database.

  • The angular service is an Angular frontend that communicates with the Express.js API server.

Each service has its own build context, which shows where the Dockerfile is for creating the images. The depends_on feature makes sure services start in the right order and can connect with one another.

You would need to create the corresponding Dockerfiles for each service (e.g., node-app/Dockerfile, express-app/Dockerfile, angular-app/Dockerfile). Additionally, each application may have its own dependencies and configurations.

Don't forget to adjust the Docker Compose file and Dockerfiles according to your unique MEAN application setup and needs.

Did you find this article valuable?

Support LingarajTechhub by becoming a sponsor. Any amount is appreciated!