📜  docker-compose mongodb 副本 (1)

📅  最后修改于: 2023-12-03 15:30:32.453000             🧑  作者: Mango

Introduction to Docker Compose with MongoDB Replication

Docker Compose is a tool for defining and running multi-container Docker applications. It allows developers to easily specify the services and networks that make up an application and then start them with a single command. MongoDB is a popular NoSQL database that stores data as documents. MongoDB replication is a feature that allows data to be copied automatically from one MongoDB server to another.

This tutorial will guide you through setting up a Docker Compose file that includes a MongoDB replication setup.

Prerequisites

Before starting, you should have the following:

Docker Compose File

To set up a MongoDB replication with Docker Compose, we will create a docker-compose.yaml file with the following contents:

version: "3.8"

services:
  mongo1:
    image: mongo
    command: mongod --replSet rs0 --bind_ip_all
    ports:
      - "27017:27017"
    volumes:
      - mongo1:/data/db
    entrypoint: /bin/bash -c "sleep 10 && /usr/local/bin/init-replica-set.sh"
  
  mongo2:
    image: mongo
    command: mongod --replSet rs0 --bind_ip_all
    volumes:
      - mongo2:/data/db
    entrypoint: /bin/bash -c "sleep 10 && /usr/local/bin/add-to-replica-set.sh mongo1:27017"
  
  mongo3:
    image: mongo
    command: mongod --replSet rs0 --bind_ip_all
    volumes:
      - mongo3:/data/db
    entrypoint: /bin/bash -c "sleep 10 && /usr/local/bin/add-to-replica-set.sh mongo1:27017"

volumes:
  mongo1:
  mongo2:
  mongo3:

In this file, we define three MongoDB replicas, each of which is a separate Docker container. We use the mongo image to create these containers. Each container also has a named volume to persist data.

We set the command value for each container to mongod --replSet rs0 --bind_ip_all which tells MongoDB to start in replication mode and bind to all available interfaces.

We expose port 27017 in the first container mongo1 as we will use it to connect to this container from our application code.

We also specify an entrypoint for each container. The entrypoint scripts will start each MongoDB instance and add it to a replica set. The init-replica-set.sh script will initialize the replica set, while the add-to-replica-set.sh script will add additional nodes to the replica set.

Running the MongoDB Replication

To start the MongoDB replication, we run the following command in the same directory as the docker-compose.yaml file:

docker-compose up -d

This will start the MongoDB instances in the background as separate Docker containers.

To check that the containers are running, run the following command:

docker-compose ps

This should output something similar to the following:

   Name                Command               State           Ports
------------------------------------------------------------------------------
docker-compose_mongo1_1   /bin/bash -c sleep 10 &&  ...   Up      0.0.0.0:27017->27017/tcp
docker-compose_mongo2_1   /bin/bash -c sleep 10 &&  ...   Up
docker-compose_mongo3_1   /bin/bash -c sleep 10 &&  ...   Up
Conclusion

In this tutorial, we have introduced Docker Compose and MongoDB replication. We have shown how to use Docker Compose to create a MongoDB replication setup using three containers. With this setup, you can easily scale your MongoDB cluster by adding more nodes to the replica set.

For more information, see the official Docker Compose documentation and the official MongoDB documentation.