Search code examples
docker

Mongo-express from Docker not connecting to Mongo


I know this question has been asked before and I've tried going through all the answers with no success. To start off, I am new to Docker so please forgive me if something obvious was missed. Whether in docker-compose or cmd line, I can't get mongo-express to connect to Mongo DB.
Below I'm only asking about the cmd line as I would think I need to at least get that working.

Env: Windows 10 Pro with WSL2 / Ubuntu 20.04 and Docker Desktop version 20.10

mongo cmd line:

docker run -d --name mongo-dev -p 27017:27017 -v ~/mongodata:/data/db \
-e MONGO_INITDB_ROOT_USERNAME='admin' -e MONGO_INITDB_DATABASE='auth' \
-e MONGO_INITDB_ROOT_PASSWORD='pass' --hostname mongo-dev --network bridge mongo

I wait for it to fully start by checking logs:

{"t":{"$date":"2020-12-30T16:14:35.373+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

Then run mongo-express with the cmd line:

docker run -d --name me -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME='admin' \
-e ME_CONFIG_MONGODB_ADMINPASSWORD='pass' -e ME_CONFIG_MONGODB_SERVER='mongo-dev' \
-e ME_CONFIG_MONGODB_PORT=27017 --hostname me --network bridge mongo-express

mongo-express logs:

Wed Dec 30 16:44:02 UTC 2020 retrying to connect to mongo-dev:27017 (5/5)
/docker-entrypoint.sh: line 14: mongo-dev: Name does not resolve
/docker-entrypoint.sh: line 14: /dev/tcp/mongo-dev/27017: Invalid argument
...
Error [MongoError]: failed to connect to server [mongo-dev:27017] on first connect

In the few seconds that mongo-express is running, I tried: docker exec me ping mongo-dev and get "ping: bad address 'mongo-dev'" (docker exec me ping me does work)

Can anyone help?

I believe my question is less about mongo-express and more about why "mongo-dev" can't be resolved within the mongo-express container.

Thanks!


Solution

  • I think you need to create a custom network of type bridge as mentioned in the official documentation. All the containers, attached to the custom network, are by default linked by name in a custom network. However, if you want to go with the option --network bridge, which is default, then you have to use the legacy --link flag to link the containers explicitly, as mentioned in the official documentation.

    I am adding commands for the custom network option below:

    docker network create mongo-net
    
    docker run -d --name mongo-dev -p 27017:27017 -v ~/mongodata:/data/db \
    -e MONGO_INITDB_ROOT_USERNAME='admin' -e MONGO_INITDB_DATABASE='auth' \
    -e MONGO_INITDB_ROOT_PASSWORD='pass' --hostname mongo-dev --network mongo-net mongo
    
    docker run -d --name me -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME='admin' \
    -e ME_CONFIG_MONGODB_ADMINPASSWORD='pass' -e ME_CONFIG_MONGODB_SERVER='mongo-dev' \
    -e ME_CONFIG_MONGODB_PORT=27017 --hostname me --network mongo-net mongo-express
    

    I have also verified that the mongo-express container, me, has connected successfully to the mongo-dev db container.

    docker logs me has given the below output.

    Waiting for mongo-dev:27017...
    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)
    basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
    Database connected
    Admin Database connected
    

    Have fun with Docker ;)