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

时间:2017-12-20 08:18:35

标签: mongodb docker-compose

我尝试通过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) 

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

9 个答案:

答案 0 :(得分:2)

Mongo-express更改了用户并传递了ENV关键字:

OLD:

- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}

新:

 - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
 - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}

参考: https://hub.docker.com/_/mongo-express

答案 1 :(得分:2)

mongo-express的新版本中有一些更新,已接受的解决方案不再起作用,尤其是您需要解决此问题

TypeError: Cannot read property 'listDatabases' of undefined

提示

请记住,如果要调整设置,请使用选项--force-recreate重新启动。

docker-compose up --force-recreate -d

这是我当前正在使用的docker-compose.yml。

version: '3'

services:
    mongo:
        image: mongo:${MONGO_TAG}
        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=true
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
            - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
            - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
            - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
        links:
            - mongo
        ports:
          - "8081:8081"

答案 2 :(得分: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"

祝你好运!

答案 3 :(得分: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/

答案 4 :(得分:1)

万一您仍然遇到此问题...或者万一有人疯狂地试图弄清楚为什么这些都不起作用...

运行docker-compose up --force-recreate

没有完全重新创建容器(不确定为什么)-看起来确实如此,但是我发现当我进入没有Auth的数据库时,它不是在重新创建容器的数据(数据库!),并且我一个小时前创建的收藏还在那里。

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

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

一旦我这样做了,并完成了从.env文件中提取值的配置的“上调”工作-它完成了INITDB ...并在数据库中正确设置了管理员密码!

因此,如果您曾经使用任何配置对其进行创建以对其进行测试,则将有一个容器,该容器正在为其重复使用数据(我认为它不会为避免数据丢失而重新创建它-我的意思是说,它的99.9%您只想重新创建PROGRAM部分,而不是数据-否则您将丢失数据库。)

花了几个小时才解决这个问题...

答案 5 :(得分:1)

虽然这个帖子有点老了。我想分享对我有用的 docker-compose.yml。

下面启动了一个 mongoDB 实例和 mongo-express。

CREATE DEFINER=`NodeApi`@`%` PROCEDURE `View_Data`(IN tablenamenew varchar(30),IN apitoken varchar(500),OUT err_id varchar (10))
BEGIN
if exists (select 1 from employee where api_token = apitoken) then
SET @t1 =CONCAT('SELECT * FROM ',tablenamenew);
 PREPARE stmt3 FROM @t1;
 EXECUTE stmt3;
 DEALLOCATE PREPARE stmt3;
else
set err_id = "-1";
select err_id;
end if;
END

答案 6 :(得分:0)

它可以在我的Mac中使用。如果删除docker-compose容器并重建,也许可以。我添加了文件。只有我更改了mongo-express端口。

.env

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

docker-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

答案 7 :(得分:0)

这是非常有用的东西,但同时不稳定。您需要找出自己的方式。如何为我docker-compose.yml写东西{p>

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 

答案 8 :(得分:0)

当您的数据库使用卷配置时会出现此问题,但 mongodb 和 mongo-express 不共享网络配置。 这对我来说非常有效:

version: "3.7"

services:
  db:
    container_name: mongo-dev
    image: mongo:4.2
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_DATABASE=auth
      - MONGO_INITDB_ROOT_PASSWORD=pass
    networks: 
      - mongo-compose-network
    ports:
      - '27017:27017'
    volumes: 
      - ./data:/data/db
  
  mongo-express:
    container_name: mongo-express
    image: mongo-express
    depends_on:
      - db
    networks: 
      - mongo-compose-network
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo-dev
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=pass
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=tribes
    ports:
      - '8081:8081'
    volumes: 
      - ./data:/data/db      

networks:
  mongo-compose-network:
    driver: bridge