如何使用docker-compose运行MongoDB和Mongo-express?

问题描述 投票:6回答:5

我尝试通过Docker-compose运行MongoDB和Mongo-express。我使用以下配置:

version: '3'

services:
    mongo:
        image: mongo
        environment:
            - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
            - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
            - MONGO_INITDB_DATABASE=project
    mongo-express:
        image: mongo-express
        environment:
            - ME_CONFIG_MONGODB_SERVER=mongo
            - ME_CONFIG_MONGODB_PORT=27017
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=false
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
            - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
            - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
            - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
        depends_on:
            - mongo
        ports:
          - "8080:8081"

和.env文件:

MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev

docker-compose up mongo-service运行之后,mongo-express失败了。我在日志中看到错误:

mongo-express_1  | Welcome to mongo-express
mongo-express_1  | ------------------------
mongo-express_1  | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1  | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1  | Database connected
mongo-express_1  | Connecting to admin...
mongo-express_1  | TypeError: db is not a function
mongo-express_1  |     at /node_modules/mongo-express/lib/db.js:114:53
mongo-express_1  |     at Array.forEach (native) 

我做错了什么?谢谢你提前!

mongodb docker-compose
5个回答
1
投票

像这样运行它对我来说也许对你有所帮助

      mongoex:
        image: mongo-express
        environment:
        - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
        - ME_CONFIG_MONGODB_SERVER=database
        - ME_CONFIG_MONGODB_PORT=27017
        - ME_CONFIG_MONGODB_ENABLE_ADMIN=false
        - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
        ports:
        - "8081:8081"
        links:
        - database
      database:
        image: mongo:latest
        command: --smallfiles
        ports:
          - "27017:27017"

祝你好运和问候!


1
投票

最简单的方法是使用docker-compose.yml在目录中运行docker-compose up -d

version: "3"
services:
  mongo:
    image: "mongo:3-stretch"
  mongo-express:
    image: "mongo-express:latest"
    ports:
      - "8081:8081"

然后在浏览器中打开http://localhost:8081/


1
投票

万一你仍然遇到这个问题...或者万一其他人绝对疯狂试图弄清楚为什么没有这个...

运行docker-compose up --force-recreate

不完全重新创建容器(不确定原因) - 它看起来像它,但我发现当我进入没有Auth的数据库时,它没有重新创建容器的数据(数据库!),以及一个集合我创建一小时前还在那里。

我已经运行了上面的命令,还有'docker image rm mongo'以及删除mongo-express(不需要)

问题是我必须做一个'docker container list'来查看我拥有的两个容器(一个用于Mongo本身,一个用于Mongo用Mongo-express - 不确定我需要删除哪个,但我删除了它们两个.. 。)

一旦我这样做,并且'up'你的配置拉动.env文件中的值工作正常 - 它做了INITDB ...并在数据库中正确设置管理员密码!

因此,如果您使用任何配置创建它来测试它,您将拥有一个重用数据的容器(我认为它不会重新创建它以避免数据丢失 - 我的意思是,99.9%的时间你只是想要重新创建PROGRAM部分,而不是数据 - 否则您将丢失数据库。)

杀了几个小时搞清楚这个......


0
投票

它适用于我的mac。如果删除docker-compose容器并重建,也许它会起作用。我添加了我的文件。只有我改变了mongo-express端口。

.ENV

MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev

泊坞窗,compose.yml

version: '3'

services:
    mongo:
        image: mongo
        environment:
            - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
            - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
            - MONGO_INITDB_DATABASE=project
    mongo-express:
        image: mongo-express
        environment:
            - ME_CONFIG_MONGODB_SERVER=mongo
            - ME_CONFIG_MONGODB_PORT=27017
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=false
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
            - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
            - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
            - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
        depends_on:
            - mongo
        ports:
          - "8888:8081"

Docker版本

Client:
 Version:           18.06.0-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        0ffa825
 Built:             Wed Jul 18 19:05:26 2018
 OS/Arch:           darwin/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.0-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0ffa825
  Built:            Wed Jul 18 19:13:46 2018
  OS/Arch:          linux/amd64
  Experimental:     true

0
投票

这是非常有用的东西,但同时不稳定。你需要找到自己的方式如何写docker-compose.yml对我来说这是有效的

version: '3'
services:
  db:
    image: mongo:2.6.12
    container_name: app_db
    volumes:
      - store:/data/db
    ports:
      - "27017:27017"
  mongo-express:
    container_name: mongo-express
    links:
      - 'db:mongo'
    ports:
      - '8081:8081'
    environment:
      - 'ME_CONFIG_OPTIONS_EDITORTHEME=ambiance'
      - 'ME_CONFIG_BASICAUTH_USERNAME=user'
      - 'ME_CONFIG_BASICAUTH_PASSWORD=pass'
    image: mongo-express 
© www.soinside.com 2019 - 2024. All rights reserved.