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
- 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 codeMongo 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
Publicar un comentario