---For Docker desktop on windows (on linux containers)---
Postman Error -"message": "java.net.ConnectException: Connection refused (Connection refused)".
Docker compose log- productservice | com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
Spring properties -------
spring.data.mongodb.database=product
#spring.data.mongodb.username=root
#spring.data.mongodb.password=root
spring.data.mongodb.port=27017
#spring.data.mongodb.host=localhost
spring.data.mongodb.host=api-database
#spring.data.mongodb.host=host.docker.internal
Dockerfile ---------
FROM openjdk:8-jre-slim
COPY ./target/ProductService-0.0.1-SNAPSHOT.jar /usr/src/ProductService/
WORKDIR /usr/src/ProductService
EXPOSE 8105
CMD ["java", "-Dspring.data.mongodb.uri=mongodb://api-database:27017/api-database","-Djava.security.egd=file:/dev/./urandom","-jar","./ProductService-0.0.1-SNAPSHOT.jar"]
Docker-compose.yml -----
version: '3'
services:
product-server:
container_name: productservice
image: productservice
expose:
- 8105
ports:
- 8105:8105
links:
- api-database
depends_on:
- api-database
networks:
- backend
api-database:
image:mongo
container_name: api-database
restart: always
hostname: api-database
networks: - backend
networks:
backend:
driver: "bridge"
Related
when I launch my application using docker-compose, I get an error that my application cannot connect to the database, although the port is exposed and they are in the same network...
This is my docker-compose.yml file:
version: '3'
volumes:
db-data:
driver: local
mongo-config:
driver: local
services:
pulseq-mongodb:
image: mongo:latest
container_name: server-mongodb
restart: always
networks:
- server-net
ports:
- "27017:27017"
expose:
- 27017
volumes:
- db-data:/data/db
- mongo-config:/data/configdb
server:
image: my-server:0.0.1-pre-alpha.1
container_name: server
restart: always
networks:
- server-net
ports:
- "8080:8080"
depends_on:
- server-mongodb
networks:
server-net:
driver: bridge
I'm getting the following error on startup:
server | 2021-11-01 13:05:10.409 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
server |
server | com.mongodb.MongoSocketOpenException: Exception opening socket
server | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar:na]
server | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
server | at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
I tried to use many solutions, but nothing helped me. Any answer will be helpful.
Thanks.
I fixed this by using the container name, instead of localhost in the application configuration.
I am trying to configure postgres to run with springboot and metabase. Each service is running separately alone but when I try to put the 3 together in a docker-compose file, I am getting the following error :
Caused by: org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
metabase-container | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
However, I have mapped the port 5432 of db to the port 5432 of the metabase container.
And yet, It doesn't seem to work. Any help on this issue? (please find my docker-compose file below)
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=compose-postgres
- SPRING_DATASOURCE_PASSWORD=same
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=********
- METABASE_PASSWORD=same
ports:
- "8078:5432"
- "54320:5432"
metabase:
container_name: metabase-container
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
- "5432:5432"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=db
- MB_DB_PORT=5432
- MB_DB_USER=compose-postgres
- MB_DB_PASS=same
- MB_DB_HOST=0.0.0.0
- MB_ENCRYPTION_SECRET_KEY=********
Try changing MB_DB_HOST in the metabase environmental variables from 0.0.0.0 to db.
The subtilty I hadn't understood is that all the services are in the same network. Hence, deleting port forwarding (which exposes the services to outside components but is irrelevant for communication within the container) between the services and changing the Metabase configuration file does the job. Here is the modified docker-compose.yml file :
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=********
- SPRING_DATASOURCE_PASSWORD=**********
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=compose-postgres
- METABASE_PASSWORD=compose-postgres
ports:
- "8078:5432"
metabase:
container_name: metabase-container
depends_on:
- db
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=********
- MB_DB_PORT=5432
- MB_DB_USER=************
- MB_DB_PASS=compose-postgres
- MB_DB_HOST=db
- MB_ENCRYPTION_SECRET_KEY=***********
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?
I'm trying to configure my docker to pgadmin4 in my nest js project, to do the backend, but when i try connect, show this message "Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting"
I already tried configure the docker-compose.yml with pgadmin configurations, network configurations and
my docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "12345678"
ports:
- "5432:5432"
volumes:
- ./data/pgadmin/
networks:
- pg-network
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "monnerat#monnerat.com.br"
PGADMIN_DEFAULT_PASSWORD: "abc"
ports:
- "5050:80"
depends_on:
- db
networks:
- pg-network
networks:
pg-network:
driver: bridge
My actual result is connection refused is server running on host "localhost (127.0.0.1) and accepting"
but i expect he open the pgadmin to create the database
Your docker-compose.yml is good. Please see the attached host name should be 'db'
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.)