I have two containers mongo and mongo-express in the same network, the issue here is that this setup works fine on windows/ubuntu but for some reason on redhat linux mongo-express can not connect to mongo even though they are in the same network.
below are the compose file and Dockerfile i am using
services:
mongo:
container_name: mongo
build:
context: ./mongo
dockerfile: Dockerfile
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
volumes:
- mongo:/bitnami/mongodb
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
networks:
- mongoNetwork
mongo-init:
container_name: mongo-init
image: mongo:7.0
depends_on:
mongo:
condition: service_healthy
entrypoint: "mongosh --host mongo -u root -p root --authenticationDatabase admin --eval \"rs.initiate({_id: 'mongo',members: [{_id: 0, host: 'mongo'}]})\""
networks:
- mongoNetwork
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8081:8081
depends_on:
mongo:
condition: service_healthy
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_URL: mongodb://root:root@mongo:27017/lhivedb?authMechanism=SCRAM-SHA-256&authSource=admin&replicaSet=mongo
ME_CONFIG_BASICAUTH: false
networks:
- mongoNetwork
FROM mongo:8.0-rc
WORKDIR /app
# generating the keyfile for replicaset
RUN openssl rand -base64 756 > keyFile
# changing the permission and ownership of the key file
RUN chmod 400 /app/keyFile
RUN chown mongodb:mongodb /app/keyFile
# Command to start MongoDB with the specified options
CMD ["mongod", "--bind_ip_all", "--port", "27017", "--replSet", "mongo", "--keyFile", "/app/keyFile"]
logs:
root@ksfilesvr Mongo]# docker logs -f mongo-express
Waiting for mongo:27017...
/docker-entrypoint.sh: connect: Operation timed out
/docker-entrypoint.sh: line 15: /dev/tcp/mongo/27017: Operation timed out
Mon Aug 19 16:44:28 UTC 2024 retrying to connect to mongo:27017 (2/10)
^Ccontext canceled
[root@ksfilesvr Mongo]# docker logs -f mongo-init
MongoServerSelectionError: Server selection timed out after 30000 ms
[root@ksfilesvr Mongo]#
docker network inspect
[
{
"Name": "mongo_mongoNetwork",
"Id": "e886244e7da2ccda3f46ef048ad0d064ae46b9c052b5da3b773a93d164ac5387",
"Created": "2024-08-20T01:42:04.024591421+09:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2f43ccee0999d8bf52e3ee7f120d87c82687ab9223da5153c0e00c89a3cc52b4": {
"Name": "mongo",
"EndpointID": "73b8779696a8d5f15a222c10492476b4f7a24e3b9ea3a46876619d81887ec3ad",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"32818def7ba0f5fc9381e2fc363e3d5a24492aa989d8ba464ca3f3600875dcb8": {
"Name": "mongo-express",
"EndpointID": "9ac3b9ac17eb5fb07bdfac6bfe34eae509e233836b32872a9b4b0b7bbf1d30cb",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"ee055db37bd437de1178866d6449767ef8f84a85c6074276926094b7bc4a555d": {
"Name": "mongo-init",
"EndpointID": "e30e79da551da3139ba662c0d83de6101126c67b7b9046f64ecd92d7618f43b2",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "mongoNetwork",
"com.docker.compose.project": "mongo",
"com.docker.compose.version": "2.29.1"
}
}
]