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 Docker containers for Adobe Experience Manager

This is a Docker tutorial for creating a docker image for the Galen framework, above is the vid and below you will find some of the steps followed. Adobe experience manager is a content management system which in a nutshell is an application that allows us to create web sites to be consumed by end users. You might be familiar with other such applications like wordpress or drupal which serves the same purpose A typical deployment would be comprised of two AEM instances, the author instance used for creating and modifying content, the publish instance which serves the content and finally we have a dispatcher which is a static web server used for caching, load balancing and some security purposes. We can configure an AEM instance to work as an author or publish instance by either changing the file name for the jar file java -jar cq-author-450…

Creating a Docker container for a NodeJs application: Express + Mongo + Docker Compose

This is a Docker tutorial for creating a NodeJs container using expressjs and mongoose, above is the vid and below you will find the steps followed.
StepsPre-reqsHave node.js installedAnd docker installed (make sure you have docker-compose as well)Create an simple node app using expressjs and mongooseModify your container and create a docker-compose fileBuild and Run your new container Create your simple node appInitialize the node appnpm initInstall the dependencies for our appnpm install --save express mongoose Create the database.jsCreate the index.js Create a dockerfileInclude container with node preinstalled: FROM nodeCreate default/working directory: WORKDIR /usr/src/appCopy package.json to workdir and install dependencies (which we will need in this case😊): COPY package.json .RUN npm install Copy the rest of the app (just the index.js file in this case)COPY . .Expose the port required for reaching our expressjs appEXPOSE 3000Add a command to run when this container is star…