docker-compose安装mongo

创建目录

[root@localhost tools]# mkdir -p /root/tools/mongo/{data,conf,init}

创建初始化用户脚本

[root@localhost mongo]# cat /root/tools/mongo/init/init.sh
#!/usr/bin/env bash
echo "Creating mongo users..."

mongo admin --host localhost -u root -p 123456 --eval "db.createUser({user:'admin',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'},{role:'readWrite',db:'test'}]});"

mongo admin -u root -p 123456 << EOF
use test
db.createCollection("logs", { autoIndexId : true })
EOF
echo "Mongo users created."

docker-compose配置文件

[root@localhost mongo]# cat docker-compose.yml 
version: '3'
services:
  mongo:
    image: mongo:4.4.6
    container_name: mongo
    hostname: mongo
    restart: always
    networks:
      - db
    ports:
      - "27017:27017"
    environment:
      TZ: Asia/Shanghai
      MONGO_INITDB_DATABASE: test
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 123456
    volumes:
      - /etc/localtime:/etc/localtime
      - /root/tools/mongo/data:/data/db
      - /root/tools/mongo/conf:/data/configdb
      - /root/tools/mongo/init/:/docker-entrypoint-initdb.d/
    command: mongod
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always
    links:
      - mongo:mongo
    depends_on:
      - mongo
    networks:
      - db
    ports:
      - "27018:8081"
    environment:
      ME_CONFIG_OPTIONS_EDITORTHEME: 3024-night
      ME_CONFIG_MONGODB_SERVER: mongo
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: 123456
      ME_CONFIG_BASICAUTH_USERNAME: root
      ME_CONFIG_BASICAUTH_PASSWORD: 123456

networks:
  db:
    driver: bridge

启动服务

[root@localhost mongo]# docker-compose up -d

注意事项

初始化脚本只有再data数据目录为空时会执行,若未执行初始化脚本,可删除目录重新创建后执行

posted @ 2022-12-20 17:26  牧之丨  阅读(203)  评论(0编辑  收藏  举报