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

I already have coded my docker-compose.yaml file like above. Both runs when afterwards. However, mongo-express is in 403 forbidden error. And when I check the view logs in visual studio code. I get to see this error:

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
choopau
  • 2,209
  • 5
  • 21
  • 28
  • 2
    could it be that you actually establish the connection? at the end it says `connected`. The reason could be that your mongodb is still starting up while mongo-express is already trying to connect – japrescott Feb 21 '20 at 16:15

1 Answers1

0

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

Now maybe you got an error on Visual studio himself, that try to connect on localhost, but you need to give container IP instead of localhost, to find it use:

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

Just in case, on logs you provide, mongo-express is successfully connected to mongoDB (at the end)

J-Jacques M
  • 978
  • 6
  • 16