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 mongodbreplicaset_mongo-rs0-1_1 bash -c 'mongo --eval "rs.status();"'
  • Stop a specific running container
    • docker stop mongodbreplicaset_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

Entradas populares de este blog

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…

Creating a tensorflow.js + vue.js simple application in javascript

This is a Tensorflow.js tutorial for creating a simple application using Vue.js, above is the vid and below you will find some of the steps followed. Steps Pre-reqs Have node.js installed Create the Vue.js application using nuxt.js Add support for tensorflow.js in vue.js and add a simple model Add the simple tensorflow.js model using vue.js into github pages Create the Vue.js application using nuxt.js Install vue.js cli npm install -g vue-cli Create a base Vue.js app using the starter kit from Nuxt vue init nuxt-community/starter-template simple-vue-tensorflow Start the dev server npm run dev Create empty component components/TensorflowExample.vue Add the component into the page pages/index.vue …