简体   繁体   中英

Accessing container with name in custom bridge network

I created custom bridge network as:

docker network create test

I am running Mongo DB image in this network with following command:

docker run -d -p 27018:27017 -e MONGO_INITDB_ROOT_USERNAME=test -e MONGO_INITDB_ROOT_PASSWORD=password --net test --name=testdb mongo

This created container and I'm able to connect to this from robo3T.

Now I ran mongo-express image in same network with following command and trying to above DB:

docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=test -e ME_CONFIG_MONGODB_ADMINPASSWORD=password -e ME_CONFIG_MONGODB_PORT=27108 -e ME_CONFIG_MONGODB_SERVER=testdb --net test --name=mongo-ex mongo-express

But I'm getting following error:

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [testb:27018] on first connect [Error: getaddrinfo ENOTFOUND testdb

Can someone help me with this please?

Between containers on a shared docker network, you access the port of the container directly. Publishing the port, with the -p 27018:27017 option, creates a port forward from the host on 27018 into the container on port 27017. The publish is visible to those outside connecting to the host, but does not change the port the container sees itself internally, or how other containers connect over shared networks.

Also note if you do not wish for those external to docker to access the container port, there's no need to publish to allow access between containers on the shared network.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM