connect EHOSTUNREACH 172.24.0.2:27017 docker-compose - mongodb

I am currently unable to connect to mongo when I run docker-compose, I keep on getting the error below
connect EHOSTUNREACH 172.25.0.2:27017
Here is my docker-compose.yml file
version: "3.4"
services:
app:
container_name: checki
restart: always
build:
context: .
network: host
ports:
- "3000:9000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27018:27017"
and my db connection string is like this
"mongodb://mongo:27017/checki"
Please assist

Related

Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"

When trying to spin up a docker container with docker-compose,
I get the following error message:
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery
and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"
I noticed that mongo-express always adds a double quote to the end of my connection string, is this intentional or did I mess something up in my docker-compose file?
The file:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
networks:
- backbone
expose:
- 27017
ports:
- 27017:27017
env_file:
- ./.env
command: [--auth]
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ./data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: unless-stopped
networks:
- backbone
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=127.0.0.1
- ME_CONFIG_MONGODB_URL="mongodb://127.0.0.1:27017"
- ME_CONFIG_BASICAUTH_USERNAME=${DB_USERNAME}
- ME_CONFIG_BASICAUTH_PASSWORD=${DB_PASSWORD}
networks:
backbone:
driver: bridge
I already tried updating my version of docker/docker-compose and mongodb.
How do I resolve the error?
Or is there a workaround I could use?
In a container, localhost refers to the container itself. So when Mongo Express tries to connect to the database at localhost, it looks for the database inside the Mongo Express container.
Docker compose let's you refer to containers using their container names, so you should change your Mongo Express environment variables to
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_URL="mongodb://mongodb:27017"

DB cannot be found in Mongo container in docker

The app (produced by docker-compose up) works as expected. But when I entered the mongo container (docker exec -it mongo) I cannot find db chatmongoose.
connectionString = 'mongodb://mongo:27017/chatmongoose'
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
version: '3.7'
services:
server:
build:
context: ./server
dockerfile: Dockerfile
image: myapp-server
container_name: myapp-node-server
command: /usr/src/app/node_modules/.bin/nodemon server.js
ports:
- '5000:5000'
links:
- mongo
environment:
- NODE_ENV=development
networks:
- app-network
mongo:
container_name: mongo
image: mongo
volumes:
- data-volume:/data/db
ports:
- '27017:27017'
networks:
- app-network
client:
build:
context: ./client
dockerfile: Dockerfile
image: myapp-client
container_name: myapp-react-client
command: npm start
depends_on:
- server
ports:
- '3000:3000'
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
data-volume:
node_modules:
web-root:
driver: local
The data in app did work as expected but why I cannot find the db in container?
Could you try profile. It probably only start mongo but also volume will be valid.
docker-compose --profile mongo up
mongo:
profiles: ["mongo"]
container_name: mongo
image: mongo
volumes:
- data-volume:/data/db
ports:
- '27017:27017'
networks:
- app-network

Docker: Unable to connect one container with another on same network

I've a following docker-compose.yaml file:
version: "3"
services:
# Database
database:
image: mongo
container_name: database
restart: always
volumes: ["dbdata:/data/db"]
environment:
- MONGO_INITDB_ROOT_USERNAME=myusername
- MONGO_INITDB_ROOT_PASSWORD=mypassword
ports:
- 7000:27017
command: mongod
networks:
- webapp
# Server
server:
container_name: server
restart: always
build: ./server
volumes: ["./server:/var/www", "/var/www/node_modules"]
ports:
- "9000:3000"
depends_on:
- database
networks:
- webapp
networks:
webapp:
driver: bridge
volumes:
dbdata:
Now, I'm able to connect with my mongo database from my local windows machine through mongodb client but my server container is not able connect with database container.
I'm using following connection URI on my server to establish a connection:
mongodb://myusername:mypassword#database:7000/mydatabase
It must be noted that on my local machine I'm using the same URI to connect successfully with the database, the only difference is that I'm using localhost instead of database (container name) on my local machine.
Can you please tell me what I'm doing wrong here?

docker springboot only connects on postgres 5432 via docker-compose

Using docker to connect springboot to postgres via docker-compose. Using port 5432 on postgres works fine, if i try an port other than that it fails
working code
spring
spring.datasource.url=jdbc:postgresql://db:5432/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 5432:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
**connection refused code **
spring
spring.datasource.url=jdbc:postgresql://db:6000/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 6000:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
error:
spring-boot-rest-server | org.postgresql.util.PSQLException: Connection to db:6000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
What am i doing wrong?
You are confusing a bit the ports: your "db" container only exports 1 port: 5432. The 6000 that you put in your docker-compose is the port on localhost that you map to that container (db) on that port (5432).
You shouldn't even use the port mappings for the postgres container unless you want to connect from localhost which I guess you don't.
If you want to use another port than 5432 you need to extend the postgres Dockerfile and change the configuration so that postgres starts listening on a different port.
Hope this helps.
In other words: The port mapping configured in docker-compose has no relevancy to how the containers connect to each other. The mapping is only relevant when something/someone attempts to connect to your containers within the docker-compose from the outside. (Like from the localhost, as #Mihai remarked.)