0
version: "3.3"
services:
    mongodb:
        image: mongo:latest
        container_name: "mongo"
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: rootpassword
        ports:
            - 27017:27017
    mongo-express:
        image: mongo-express:latest
        container_name: mongoexpressweb
        environment:
            - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_AUTH_USERNAME=root
            - ME_CONFIG_MONGODB_AUTH_PASSWORD=rootpassword
            - ME_CONFIG_MONGODB_ADMINUSERNAME=root
            - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpassword
            - ME_CONFIG_BASICAUTH_USERNAME=root
            - ME_CONFIG_BASICAUTH_PASSWORD=root
        ports:
            - 8081:8081
        links:
            - mongodb
        depends_on:
            - mongodb

我已经像上面那样编写了我的 docker-compose.yaml 文件。两者都在之后运行。但是,mongo-express 出现 403 禁止错误。当我检查 Visual Studio 代码中的视图日志时。我看到这个错误:

Waiting for mongo:27017...
Waiting for mongo:27017...
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
Fri Feb 21 07:32:10 UTC 2020 retrying to connect to mongo:27017 (2/5)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
Fri Feb 21 07:32:11 UTC 2020 retrying to connect to mongo:27017 (3/5)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
Fri Feb 21 07:32:12 UTC 2020 retrying to connect to mongo:27017 (4/5)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
Fri Feb 21 07:32:13 UTC 2020 retrying to connect to mongo:27017 (5/5)
/docker-entrypoint.sh: connect: Connection refused
/docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
Welcome to mongo-express
------------------------


Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
Database connected
Admin Database connected
4

1 回答 1

0

码头工人-compose.yml

version: "3.3"

services:
    mongodb:
        image: mongo:latest
        container_name: "mongo"
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: rootpassword
        ports:
            - 27017:27017
        networks:       # Add this <--
            - random    # Add this <--
    mongo-express:
        image: mongo-express:latest
        container_name: mongoexpressweb
        environment:
            - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_AUTH_USERNAME=root
            - ME_CONFIG_MONGODB_AUTH_PASSWORD=rootpassword
            - ME_CONFIG_MONGODB_ADMINUSERNAME=root
            - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpassword
            - ME_CONFIG_BASICAUTH_USERNAME=root
            - ME_CONFIG_BASICAUTH_PASSWORD=root
        ports:
            - 8081:8081
        #links:         # you can remove this, deprecated
        #    - mongodb  # you can remove this, deprecated
        depends_on:
            - mongodb
        networks:       # Add this <--
            - random    # Add this <--

networks:   # Add this <--
    random: # Add this <--

现在也许你在 Visual Studio 上遇到了一个错误,它尝试在 localhost 上连接,但你需要提供容器 IP 而不是 localhost,才能找到它的用途:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

以防万一,在您提供的日志中,mongo-express 已成功连接到 mongoDB(最后)

于 2020-02-21T17:45:37.097 回答