别忘记了给setup.sh脚本添加可执行权限chmod +x setup.sh

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/scripts/setup.sh": permission denied: unknown

docker-compose.yaml

version: '3.8'

services:
  mongo1:
    hostname: mongo1
    container_name: mongo1
    image: mongo
    volumes:
      - ~/mongors/data1:/data/db
    networks:
      - mongors-network
    ports:
      - 27021:27017
    links:
      - mongo2
      - mongo3
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs", "" ]
  mongo2:
    container_name: mongo2
    image: mongo:4.4
    volumes:
      - ~/mongors/data2:/data/db
    networks:
      - mongors-network
    ports:
      - 27022:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs" ]
  mongo3:
    container_name: mongo3
    image: mongo:4.4
    volumes:
      - ~/mongors/data3:/data/db
    networks:
      - mongors-network
    ports:
      - 27023:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs" ]

networks:
  mongors-network:
    driver: bridge
#!/bin/bash

mongo <<EOF
  var cfg = {
    "_id": "rs",
    "version": 1,
    "members": [
      {
        "_id": 0,
        "host": "mongo-0.mongo:27017",
        "priority": 2
      },
      {
        "_id": 1,
        "host": "mongo-1.mongo:27017",
        "priority": 0
      },
      {
        "_id": 2,
        "host": "mongo-2.mongo:27017",
        "priority": 0
      }
    ]
  };
  rs.initiate(cfg, { force: true });
  //rs.reconfig(cfg, { force: true });
  rs.status();
EOF

sleep 10

mongo <<EOF
  use admin;
  admin = db.getSiblingDB("admin");
  admin.createUser({
    user: "admin",
    pwd: "password",
    roles: [{ role: "root", db: "admin" }]
  });
  db.getSiblingDB("admin").auth("admin", "password");
  rs.status();
EOF