Ir al contenido principal

Creating a Mongo replicaset using docker: Mongo replicaset + Nodejs + Docker Compose


This is a Docker tutorial for creating a Mongo replica set using docker compose and also a couple more containers to experiment with the replica set, above is the vid and below you will find some of the steps followed.

Steps

  • Pre-reqs
    • Have node.js installed
    • And docker installed (make sure you have docker-compose as well)
  • Create a container for defining configurations for a mongo instance
  • Create a container for setting up the replica set
  • Create a simple node app using expressjs and mongoose (A modified version from the previous video)
  • Create a docker-compose file with the mongo and setup containers and two additional containers for experimenting with the replica set
  • Build, Run and experiment with your new containers

Create a dockerfile for the first mongo container (not really needed but you could configure more stuff if needed)

  • Include container with mongo preinstalled: 
    • FROM mongo
  • Create default/working directory: 
    • WORKDIR /usr/src/configs
  • Copy mongo's configurations file into the container
    • COPY mongo.conf .
  • Expose the default port for a mongo container
    • EXPOSE 27017
  • Initialize the container using the config file
    • CMD ["--config", "./mongo.conf"]

Create a dockerfile for setting up the replica set

  • Include a container with mongo preinstalled so we can easily use the mongo shell:
    • FROM mongo
  • Create default/working directory: 
    • WORKDIR /usr/src/configs
  • Copy the scripts needed for configuring the replica set:  
    • COPY replicaSet.js .
    • COPY setup.sh .
  • Execute the setup shell script
    • CMD ["./setup.sh"]

Create a docker-compose.yml file

  • Version for the docker-compose file:
    • version: '3'
  • The list of services and their respective configurations: 
    • services:
  • Our first mongo container: mongo-rs0-1
    • The name of each service, they can also work as a way to identify each service and connect to them within a container (for example you could connect to the mongo database using the name of the service: mongodb://mongo-rs0-1/test):
      • mongo-rs0-1
    • The name of the image that we want to build
      • image: mongo-start
    • The folder in which the dockerfile is located (if you have everything in the same folder then just adding "build: ./" would suffice)
      • build: ./mongo-rs0-1
    • The ports to expose and to which ports on the host machine to map them (host-port:container-port)
      • ports:
              - "27017:27017"
    • Which folders from the host machine should be shared to the container.
      • volumes:
              - ./mongo-rs0-1/data:/data/db
    • Declare that we require this container to be up and running before the current one does
      • depends_on:
              - "mongo-rs0-2"
        and - "mongo-rs0-3"
  • Our database services: mongo-rs0-2 and mongo-rs0-3
    • The only difference here is that we are using the default mongo image and given that I specify the command to be executed
  • Our setup service: setup-rs
    • The name of each service:
      • setup-rs
    • The name of the image that we want to reuse from dockerhub
      • image: setup-rs
    • The folder in which the dockerfile is located
      • build: ./setup
  • An optional but useful service for browsing our database: adminmongo
    • The name of each service:
      •  adminmongo
    •  The name of the image that we want to reuse from dockerhub
      • image: "mrvautin/adminmongo"
    • The ports to expose for this particular container
      • ports:
              - "1234:1234"

Build & Run

  • Build the container:
    • docker-compose build
  • Run the container (will also build any necessary container)
    • docker-compose up
  • If you don't know the ip for your newly created node+express app use the ip used by the docker machine
    • docker-machine ip
  • Open the web app in your browser
    • http://THE-IP:3000/testFind
  • Adminmongo  UI
    • http://THE-IP:1234/
  • Start different specific services on your docker-compose file just add the name to each service
    • docker-compose up mongo-rs0-1 adminmongo
    • docker-compose up service-name-1 service-name-2 ...
  • See a list of the currently running containers in your machine
    • docker ps
  • Query for the replica set status
    • docker exec mongodb-replicaset_mongo-rs0-1_1 bash -c 'mongo --eval "rs.status();"'
  • Stop a specific running container
    • docker stop mongodb-replicaset_mongo-rs0-1_1

Resources

Github link for the code
Mongo Replication - Replica Set
Deploy a Replica Set for Testing and Development
Mongo - Docker Hub
Replication Options - Configuration File Options
Write Scripts for the mongo Shell

Comentarios

Entradas populares de este blog

Creating an AEM Application using Adobe's archetype version 23

In this tutorial, I will show how to use Adobe's archetype to create an AEM application using version 23rd and up, above is the vid and below you will find some useful notes. 1. Pre-reqs Have access to an Adobe Experience Manager instance. You will need aem 6.3 Service Pack 3, AEM 6.4 or AEM 6.5 2. Creating an AEM application using archetype version 23 Look for the .m2 folder inside your user for your particular Operating System

How to create an AEM component using Reactjs

In this tutorial, I will show how to use use Adobe's archetype to create an AEM application with React.js support and also how to add a new React.js component so that it can be added into a page, above is the vid and below you will find some useful notes. In the second part we will see how to configure the Sling Model for the AEM React component. 1. Pre-reqs Have access to an Adobe Experience Manager instance. You will need aem 6.4 Service Pack 2 or newer. Have Maven installed, understand how it works and also understand how to use Adobe's archetype, you can watch my video about maven here: Creating an AEM application using Maven and Adobe's archetype 2.

Create a custom AEM workflow process step with a dialog

In this tutorial I talk about how to create a custom workflow step process with an additional dialog for configuring it, above is the vid and below you will find some useful notes. 1. Pre-reqs Have access to an Adobe Experience Manager instance. Have Maven installed, understand how it works and also understand how to use Adobe's archetype, you can watch my video about maven here: Creating an AEM application using Maven and Adobe's archetype 2. What is an AEM Workflow and workflow model Workflows allow you to automate different tasks inside AEM by defining a s