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

How to copy files from and to a running Docker container

Sometimes you want to copy files to or from a container that doesn’t have a volume previously created, in this quick tips episode, you will learn how. Above is the vid and below you will find some useful notes. 1. Pre-reqs Have Docker installed 2. Start a Docker container For this video I will be using a Jenkins image as an example, so let’s first download it by using docker pull docker pull jenkins/jenkins:lts

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.

Integrating Nodejs and Maven using The Maven Frontend Plugin

In this tutorial I show how to integrate nodejs with maven using the Maven Frontend Plugin, above is the vid and below you will find some useful notes. 1. Pre-reqs Have access to an Adobe Experience Manager instance if you want to install the AEM application and test it. The same pom configs shown here can be used for different types of applications 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. Creating the base app