当我在使用docker-compose来使用volume时,发现mongo-express无法运行

现象

最近一段时间没有用 MongoDB,当我尝试使用它时发现之前使用的 docker-compose (mongo-express 的)不能正常工作了。

version: "3.1"

services:
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: secret
      MONGO_INITDB_DATABASE: app_marketing
    ports:
      - 27017:27017
    volumes:
      - ./configdb:/data/configdb
      - mongoDataStore:/data/db

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: user
      ME_CONFIG_MONGODB_ADMINPASSWORD: secret

volumes:
  mongoDataStore:
    driver: local

这种情况。看起来在2020年9月时是可以工作的。MongoDB是可以工作的,但Mongo-Express无法工作。

环境

Docker版本为20.10.11,Docker Compose版本为1.29.2。

使用Docker Hub官方提供的内容重新创建

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

加音量。

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - ./configdb:/data/configdb
      - mongoDataStore:/data/db

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

volumes:
  mongoDataStore:
    driver: local

不动。

原因和解决方案 hé ‘àn)

如果使用volume,则在mongo和mongo-express之间共享网络配置,否则会发生此问题。

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    container_name: mongo-dev
    image: mongo
    restart: always
    networks:
      - mongo-compose-network
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - ./configdb:/data/configdb
      - mongoDataStore:/data/db

  mongo-express:
    container_name: mongo-express
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    networks:
      - mongo-compose-network
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

networks:
  mongo-compose-network:
    driver: bridge
volumes:
  mongoDataStore:
    driver: local

这个有效果了。

广告
将在 10 秒后关闭
bannerAds