I try to figure out why the communication between dockerized Spring boot app and mongoDB is not working. First things first:
The whole microservice is running localy perfectly.
I create an application.properties with the profile name docker:
application-docker.properties
server.port=8091
spring.application.name=customer
# ============================================================================================================
# EUREKA
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "eureka-server". See the file docker-compose.yml to check the container name.
eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
# ============================================================================================================
# ZIPKIN
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "zipkin". See the file docker-compose.yml to check the container name.
spring.zipkin.base-url=http://zipkin:9411
# ============================================================================================================
# RABBIT_MQ
# It's not possible to use localhost in the docker container. Instate use the container name.
# In this case "rabbitmq". See the file docker-compose.yml to check the container name.
spring.rabbitmq.addresses=rabbitmq:5672
rabbitmq.exchanges.internal=internal.exchange
rabbitmq.queue.notification=notification.queue
rabbitmq.routing-keys.internal-notification=internal.notification.routing-key
# ============================================================================================================
# MONGO-DB
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
#spring.data.mongodb.database=data
#spring.data.mongodb.host=spring
#spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://mongodb:27017/data
and then the docker-compose.yml
services:
mongodb:
container_name: mongodb
image: mongo:latest
hostname: mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
networks:
- spring
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8095:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
networks:
- spring
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
networks:
- spring
eureka-server:
image: koulombus/eureka-server:latest
container_name: eureka-server
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: koulombus/api-gateway:latest
container_name: api-gateway
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
customer:
image: koulombus/customer:latest
container_name: customer
ports:
- 8091:8091
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
fraud:
image: koulombus/fraud:latest
container_name: fraud
ports:
- 8092:8092
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
notification:
image: koulombus/notification:latest
container_name: notification
ports:
- 8093:8093
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
volumes:
postgres:
pgadmin:
data: {}
networks:
postgres:
driver: bridge
spring:
driver: bridge
after starting docker...
mvn clean package -P "build-docker-image"
docker compose up -d
Facts
Eureka-Server:
Ziplin:
RabbitMQ:
Mongo-express:
Problem
When I check now the customer log in docker i facet following:
,-----. ,--. ,--. ,---. ,--------. ,-----. ,--. ,--. ,------. ,------.
' .--./ | | | | ' .-' '--. .--' ' .-. ' | `.' | | .---' | .--. '
| | | | | | `. `-. | | | | | | | |'.'| | | `--, | '--'.'
' '--'\ ' '-' ' .-' | | | ' '-' ' | | | | | `---. | |\ \
`-----' `-----' `-----' `--' `-----' `--' `--' `------' `--' '--'
Application Name:
Application Version:
:: Spring Boot :: (v2.6.3)
2022-03-03 08:58:55.187 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : Starting CustomerApplication using Java 17.0.1 on cf6b5b41507d with PID 1 (/app/classes started by root in /)
2022-03-03 08:58:55.241 INFO [customer,,] 1 --- [ main] c.k.customer.CustomerApplication : The following profiles are active: docker
2022-03-03 08:59:01.557 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2022-03-03 08:59:02.239 INFO [customer,,] 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 667 ms. Found 1 MongoDB repository interfaces.
2022-03-03 08:59:04.589 INFO [customer,,] 1 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=9e7fc4f4-3641-3895-b9a0-4754b711f194
2022-03-03 08:59:08.906 INFO [customer,,] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8091 (http)
2022-03-03 08:59:08.952 INFO [customer,,] 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-03-03 08:59:08.953 INFO [customer,,] 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-03-03 08:59:09.454 INFO [customer,,] 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 13490 ms
2022-03-03 08:59:12.729 INFO [customer,,] 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[mongodb:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2022-03-03 08:59:13.071 INFO [customer,,] 1 --- [}-mongodb:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server mongodb:27017
com.mongodb.MongoSocketException: mongodb
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.4.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:152) ~[mongodb-driver-core-4.4.1.jar:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.UnknownHostException: mongodb
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:887) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301) ~[na:na]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-4.4.1.jar:na]
... 6 common frames omitted
Somewhere I make a mistake. I would be very grateful for a hint.
You're missing an 's' in spring.data.mongodb.uri in your config file.
OK, working solution for me:
application-docker.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27017
spring.data.mongodb.database=data
docker-compose.yml
services:
mongodb:
container_name: mongodb
image: mongo:latest
hostname: mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
networks:
- spring
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always
ports:
- 8095:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- mongodb
networks:
- spring
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
networks:
- spring
rabbitmq:
image: rabbitmq:3.9.11-management-alpine
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
networks:
- spring
eureka-server:
image: koulombus/eureka-server:latest
container_name: eureka-server
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
api-gateway:
image: koulombus/api-gateway:latest
container_name: api-gateway
ports:
- 8090:8090
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
customer:
image: koulombus/customer:latest
container_name: customer
ports:
- 8091:8091
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
- fraud
fraud:
image: koulombus/fraud:latest
container_name: fraud
ports:
- 8092:8092
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
notification:
image: koulombus/notification:latest
container_name: notification
ports:
- 8093:8093
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- spring
depends_on:
- zipkin
- eureka-server
- rabbitmq
- mongodb
volumes:
postgres:
pgadmin:
data: {}
networks:
postgres:
driver: bridge
spring:
driver: bridge
Related
I am trying to set up SonarQube self-hosted using docker with docker-compose. To use SonarQube via HTTPS I am using nginx as reverse proxy and the jrcs/letsencrypt-nginx-proxy-companion to handle the SSL-Certificate.
Now I have configured SonarQube using the following docker-compose.yml:
version: '3'
networks:
ext:
int:
volumes:
certs:
vhosts:
html:
postgresql_data:
postgresql:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
services:
nginxproxy:
image: jwilder/nginx-proxy:alpine
container_name: nginxproxy
hostname: nginxproxy
restart:
unless-stopped
networks:
- ext
volumes:
- certs:/etc/nginx/certs:ro
- vhosts:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./config/my_proxy.conf:/etc/nginx/conf.d/myproxy.conf:ro
ports:
- 80:80
- 443:443
labels:
- com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
environment:
- SSL_POLICY=Mozilla-Intermediate
nginxproxy_comp:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginxproxy_comp
hostname: nginxproxy_comp
restart:
unless-stopped
depends_on:
- nginxproxy
networks:
- ext
volumes:
- certs:/etc/nginx/certs
- vhosts:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
sonarqube:
image: sonarqube:community
container_name: sonarqube
hostname: sonarqube
depends_on:
- db
restart:
unless-stopped
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_extensions:/opt/sonarqube/extensions
environment:
- SONAR_JDBC_URL=jdbc:postgresql://localhost:5432/sonarqube
- SONAR_JDBC_USER=sonarqube
- SONAR_JDBC_PASSWORD=sonarqube
networks:
- int
ports:
- 9000:9000
db:
image: postgres:12
container_name: db
hostname: db
networks:
- int
ports:
- 5432:5432
environment:
- POSTGRES_USER=sonarqube
- POSTGRES_PASSWORD=sonarqube
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
restart:
unless-stopped
Now when I try to start docker-compose, SonarQube is giving me the following Error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader#277050dc-org.sonar.db.DefaultDatabase': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Fail to connect to database
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Can anyone please tell me what i did wrong?
What I see here is the incorrect JDBC URL. It must below
SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
Since you have network between containers DB accessible via db
Also, under SonarQube container add below as well
privileged: true
Check one of my sample project in GitHub
https://github.com/shamith234/sonarqube-9.5-postgres-docker/blob/master/docker-compose.yml
Description
I have a container that has a mongodb, mongo-express and typesense. I need to set that typesense use data from the mongodb inside the container. How can i do this?
I already used and configured the replica set.
Steps to reproduce
This is my docker compose file:
version: "3.9"
services:
database:
image : 'mongo'
container_name: 'container-name'
environment:
- PUID=1000
- PGID=1000
volumes:
- ./database:/data/db
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
ports:
- 27017:27017
restart: unless-stopped
mongo-express:
image: mongo-express:0.54
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_SERVER: database
depends_on:
- database
typesense:
image: typesense/typesense:0.21.0
ports:
- "8108:8108"
environment:
TYPESENSE_API_KEY: xyz
TYPESENSE_DATA_DIR: /data/typesense
TYPESENSE_ENABLE_CORS: "true"
volumes:
- ./typesense:/data/typesense
When i call the API colletions/data, i get a response and on the end i see num_documents: 0
Github answer
I have created a Spring Cloud microservice ecosystem which is made up of a spring cloud eureka server, a spring cloud gateway proxy and several microservices. I have also used docker to containerize each of this services and i start up the images using a docker-compose file.
version: '3'
services:
discovery-server:
image: serviceregistry-api-docker:latest
ports:
- 8761:8761
networks:
- transaction-network
api-gateway:
image: apigateway-api-docker:latest
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery-server:8761/eureka/
depends_on:
- discovery-server
ports:
- 9091:9091
networks:
- transaction-network
utility-service:
image: utility-api-docker:latest
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery-server:8761/eureka/
restart: on-failure
depends_on:
- discovery-server
- api-gateway
ports:
- 8090:8090
networks:
- transaction-network
banktransfer-service:
image: banktransfer-api-docker:latest
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery-server:8761/eureka/
restart: on-failure
depends_on:
- discovery-server
- api-gateway
ports:
- 8091:8091
networks:
- transaction-network
ussd-service:
image: ussd-api-docker:latest
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery-server:8761/eureka/
restart: on-failure
depends_on:
- discovery-server
- api-gateway
ports:
- 8096:8096
networks:
- transaction-network
networks:
transaction-network:
driver: bridge
When i make an update to just 1 microservice and create a new docker image that has my update, how do i replace it with the current running version in the ecosystem ?
Just start it with --no-deps param (even if it already started). For example:
docker-compose up -d --no-deps api-gateway
I used this command to make it possible
$docker-compose up -d --no-deps --build <service_name>
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'am trying to use docker-compose with a springboot app(uses JPA) and a postgres database, I am kinda new using docker and I don't know what I am doing wrong.
Problem
When I build the springboot app, if I change
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres(code that works) to host it in postgres container, spring.datasource.url=jdbc:postgresql://postgres:5432/postgres.
It throws me a PSQLException caused by: java.net.UnknownHostException: postgres.(More details below)
application.properties
server.port = 8080
## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
## PostgreSQL
spring.datasource.url=jdbc:postgresql://postgres:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create
Exception
2020-06-22 19:07:10.597 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : Starting AnnotationToolApplicationTests on DESKTOP-P1E07PA with PID 16088 (started by Pedro in C:\Users\Pedro\git\tfg\annotation-tool\annotation-tool)
2020-06-22 19:07:10.598 INFO 16088 --- [ main] c.a.a.AnnotationToolApplicationTests : No active profile set, falling back to default profiles: default
2020-06-22 19:07:11.584 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-22 19:07:11.719 INFO 16088 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 120ms. Found 3 JPA repository interfaces.
2020-06-22 19:07:12.235 INFO 16088 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-22 19:07:12.572 INFO 16088 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-22 19:07:12.746 INFO 16088 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-06-22 19:07:13.002 INFO 16088 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-22 19:07:13.434 INFO 16088 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-06-22 19:07:16.873 ERROR 16088 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.9.jar:42.2.9]
...
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: java.net.UnknownHostException: postgres
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_222]
docker-compose.yml
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
volumes:
apiDB:
Api Dockerfile
FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package
FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Things that I think that happened/ tried
Maybe the DNS is not working, but I don't know how I can solve it
I tried differents docker-compose and Dockerfiles and it doesn't work
If I run the docker-compose the postgres container is created succesfully
I tried changing the url host to postgres-backend as my container was named that way
I tried by using links between my components.
I think springboot application is finding postgres db before its container is getting up...you can add depends_on property in springboot container ..so the postgres container starts first then springboot
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
volumes:
apiDB:
I don't know if I am thinking to simple right know but all container of your docker-compose file run on the same host, right? So then your url should point to jdbc:postgresql://localhost:5432/postgres.
I don't know if you already solved the problem but have you tried to use a bridge network on the setup?
version: '3'
services:
# SpringBoot App
postgres:
image: "postgres:9.6-alpine"
ports:
- 5432:5432
volumes:
- apiDB:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
networks:
- database-network
api:
build: ./annotation-tool
ports:
- 8080:8080
depends_on:
- postgres
networks:
- database-network
client:
container_name: client-container
build: ./client
ports:
- 8081:8081
networks:
database-network:
driver: bridge
external: false
volumes:
apiDB: