I have a problem with the docker and containerized apps not connecting to mysql database that is running in a container within the same docker network. My docker-compose.yml looks like this:
version: '3.8'
services:
mysqlserver:
image: mysql:5.7.24
container_name: mysql_app
networks:
- somenetwork
ports:
- "5647:3306"
environment:
MYSQL_DATABASE: database
MYSQL_USER: USER
MYSQL_PASSWORD: PASS
MYSQL_ROOT_PASSWORD: PASS
command:
- --max_connections=1000
restart: always
volumes:
- persistent:/var/lib/mysql
app:
image: app/appserver:latest
container_name: app_server
networks:
- somenetwork
ports:
- "8080:8080"
depends_on:
- mysqlserver
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:5647/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true
networks:
somenetwork:
name:
somenetwork_share_net
volumes:
persistent:
And looking at the logs from the app container, I found errors like this:
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
and
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
Is there something wrong with my docker-compose.yml? Why the app container cannot connect
You have to use the exposed port on container side 3303 and not the public one 5647.
Just try
SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:3306/database?verifyServerCertificate=false&useSSL=false&requireSSL=false&autoReconnect=true
The port in local network stays the original exposed port. In port section you just define the public ports on the host machine.
Related
I am trying to run all microservices from a docker-compose file and I am facing issues when running the containers due to the issue that have between the Eureka discovery server and api-gateway with the other services. There is a way that can make the discover-server (Eureka) communicate with the other services? Many thanks in advance.
version: "3"
services:
discovery-server:
image: renosbardis/discovery-service:latest
container_name: discovery-server
ports:
- "8761:8761"
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- test-network
api-gateway:
image: renosbardis/api-gateway:latest
container_name: api-gateway
ports:
- "8888:8888"
environment:
- SPRING_PROFILES_ACTIVE=docker
- LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_SECURITY= TRACE
depends_on:
- discovery-server
networks:
- test-network
accounts-service:
image: renosbardis/accounts-service:latest
container_name: accounts-service
ports:
- "8081:8081"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: rabbitmq
ports:
- "5672:5672"
- "15672:15672"
environment:
AMQP_URL: 'amqp://rabbitmq?connection_attempts=5&retry_delay=5'
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
customers-service:
image: renosbardis/customer-service:latest
container_name: customers-service
ports:
- "8083:8083"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
transactions-service:
image: renosbardis/transaction-service:latest
container_name: transactions-service
ports:
- "8084:8084"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
notification-service:
image: renosbardis/notification-service:latest
container_name: notification-service
ports:
- "8085:8085"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
networks:
test-network:
driver: bridge
There is nothing wrong with the docker-compose configuration file, the first thing you need to check is that the api-gateway can access the discovery-server properly
You can go into the containers and ping or telnet to test whether the network between the containers is connected.
use docker exec -it name /bin/bash
The startup log shows that your api-gateway configuration is incorrect, as it should be
discover-server:8761//eureka no localhost
api-gateway | 2022-12-28 02:25:27.549 INFO 1 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=I/O error on POST request for "http://localhost:8761/eureka/apps/API-GATEWAY": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused) stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8761/eureka/apps/API-GATEWAY": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
edit 1
I pulled your api-gateway image and started
And executed
docker cp api-gateway:/app/resources/application.properties ./
Here is the configuration that went wrong
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
You just have to change it to
eureka.client.serviceUrl.defaultZone=http://discovery-server:8761/eureka
edit 2
Add the following configuration to api-gatway configuration file to see if it works
eureka.client.fetch-registry=true
eureka.client.registry-with-eureka=true
edit 3
I find that most of your problems are configuration file errors in the mirror
This section uses customer-service mirroring as an example
Its port is set to 0
hostname is localhost
defaultZone is localhost:8761/eureka
This is the configuration file of customer-service that I modified
# Server port
server.port = 8083
eureka.instance.hostname = customer-service
spring.application.name = customer-service
# Memory Database for development Environment
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:customerdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.hibernate.dialect=org.hibernate.dialect.H2Dialect
eureka.client.service-url.defaultZone=http://discovery-server:8761/eureka
eureka.client.fetch-registry= true
eureka.client.register-with-eureka= true
This is my visit 0.0.0.0:8888/api/v1/customer/2 the returned json all working properly
{
"customerID": 2,
"name": "John",
"surname": "Doe",
"balance": 50
}
I'm stuck with SQLNonTransientConnectionException in Spring Boot application with MariaDB database using docker.
Docker-compose.yaml
version: '3.1'
services:
stats-server:
container_name: stats-server
build:
dockerfile: Dockerfile-statsserver
context: ./stats-server
ports:
- "9090:9090"
depends_on:
- stats-db
stats-db:
container_name: stats-db
image: mariadb
ports:
- "9091:3306"
environment:
- MARIADB_ROOT_PASSWORD=root
- MARIADB_DATABASE=stats
- MARIADB_USER=stats_user
- MARIADB_PASSWORD=stats_password
application.properties
#port
server.port=9090
#MariaDB
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://stats-db:9091/stats
spring.datasource.username=stats_user
spring.datasource.password=stats_password
spring.jpa.hibernate.ddl-auto=update
After docker-compose up command I've got the SQLNonTransientConnectionException error:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=stats-db)(port=9091)(type=primary). Connection refused
Database container runs well.
With Intellij Idea I can connect by this url:
jdbc:mariadb://localhost:9091/stats
my bad, should been use internal container port
spring.datasource.url=jdbc:mariadb://stats-db:3306/stats
I have two docker containers: mysql and spring boot service.
Service container is not connecting to mysql container(failing on the deploy) with following exception:
CommunicationsException: Communications link failure
So far I have tried using docker --link, docker networks, running everything using/without docker compose.
I am able to connect to DB container outside of docker.
Connection string: jdbc:mysql://db:3306/somedb
docker-compose.yml
version: '3.7'
services:
app:
build: .
restart: unless-stopped
ports:
- "80:8060"
links:
- db
depends_on:
- db
db:
image: "mysql:latest"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_DATABASE=somedb
- MYSQL_ROOT_PASSWORD=somepassword
ports:
- "3306:3306"
volumes:
- /Users/someuser/someproject/mysql/data:/var/lib/mysql
I had the same problem earlier and i tried with by changing the mysql image version. Here is my compose yaml file where i am able to connect it with.
version: "3.0"
services:
spring-boot-container:
build: .
ports:
- "8086:8086"
links:
- mysql-standalone
mysql-standalone:
image: mysql:5.6
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: springsecurity
MYSQL_USER: sa
MYSQL_PASSWORD: password
So in my service dockerfile I say:
ADD target/app-1.0-SNAPSHOT.jar /app-service.jar
RUN sh -c 'touch /app-service.jar'
But I forgot to rebuild jar after making changes in service config files
TL;DR I'm retarded
I am trying to set up a Spring boot application with Redis Sentinel 3.2.11 using docker. However I am getting
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /172.27.0.2:6379
My docker compose configuration
version: '3.1'
services:
master:
image: redis:3
container_name: redis-master
hostname: host_dev
networks:
- docker_dev
slave:
image: redis:3
command: redis-server --slaveof redis-master 6379
hostname: host_dev
links:
- master:redis-master
container_name: redis-slave
networks:
- docker_dev
sentinel:
build: sentinel
environment:
- SENTINEL_DOWN_AFTER=5000
- SENTINEL_FAILOVER=5000
- MASTER_NAME=mymaster
hostname: host_dev
image: sentinel:3
links:
- master:redis-master
- slave
container_name: sentinel
ports:
- "26379:26379"
networks:
- docker_dev
networks:
docker_dev:
Docker file
FROM redis:3
EXPOSE 26379
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 30000
ENV SENTINEL_FAILOVER 180000
COPY sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]
Spring configuration in application.properties:
redis.cluster.name=mymaster
redis.sentinel.nodes=localhost:26379
redis.timeout=2000
Issue:
The spring boot app(run from outside docker-machine) is able to connect with Sentinel node. The sentinel node provides the master information with IP 172.27.0.2 i.e docker n/w IP. The spring boot app tries to connect with redis-master at IP 172.27.0.2 and fails as the IP is not visible outside the docker machine.
Possible fix:
How can I make sentinel node provide an master IP as localhost instead of internal docker-machine n/w ip?
I try to starting my application(Spring Boot + Spring Cloud + Eureka + MongoDB) with using docker image, but i can't get connection to MongoDB.
Exception:
exception "com.mongodb.MongoSocketOpenException: Exception opening socket."
I starting my application with execute command: docker-compose up --build
Docker log:
com.mongodb. MongoSocket0penException: Exception opening socket at com.mongodb. connection. SocketStream.open(SocketStream.java: 63) ~|mongo-java-driver-3.2.2.jar!/:naj
at com. monsoob: connection. berettaberverwontortser/ersonito-kunnablearancerainitservertonitolnava:1203-dionso-java-driver*3?2.2.jarl/:nal
at : java. lang. Thread.run (Thread. java: 745) Lna:1.8.0_111]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect (Native Method) ~ [na: 1.8.0_1111 at java.net.AbstractPlainSocketImp1.doConnect (AbstractplainSocketImpl.java:350) ~ [na: 1.8.0_111]
at java.net.AbstractPlainSocketImp1.connectToAddress(AbstractPlainSocketImp1.java:206)~[na:1.8.0_1111
at java.net .AbstractPlainSocketImp1.connect (AbstractPlain5ocket Imp],java:188) ~[na:1.8.0_111]
at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392) ~|na:1.8.0_111] java.net Socket. connect (Socket, java: 589) ~[na:1.8:0-111]
at com.mongodb.connection. SocketStreamHelper.initialize(SocketStreamHelper. java: 50) ~ [mongo-java-driver-3.2.2.ar!/:na] at com. mongodb. connection. SocketStream.open(SocketStream. java:58) ~ [mongo-java-driver-3.2.2.jar!/:na]
3 common frames omitted
application.yml:
# Spring properties
spring:
application:
name: car-service
data:
mongodb.host: localhost
mongodb.port: 32769
mongodb.uri: mongodb://localhost/test
mongo.repositories.enabled: true
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# HTTP Server (Tomcat) Port
server:
port: 2220
error:
whitelabel:
enabled: false
docker-compose.yml:
eureka:
build: ./eureka-discovery-service
ports:
- "8761:8761"
mongodb:
image: mongo:3.0.4
ports:
- "32769:32769"
postgresql:
image: postgres:9.6.1
ports:
- "32770:32770"
gateway-service:
build: ./gateway-service
ports:
- "9090:9090"
links:
- eureka
environment:
SPRING_APPLICATION_NAME: gateway-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
airplane-service:
build: ./airplane-service
ports:
- "2222:2222"
links:
- eureka
- postgresql
environment:
SPRING_APPLICATION_NAME: airplane-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
SPRING_DATASOURCE_POSTGRESQL_URL: jdbc:postgresql://localhost:32770/postgres
car-service:
build: ./car-service
ports:
- "2220:2220"
links:
- eureka
- mongodb
environment:
SPRING_APPLICATION_NAME: car-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
SPRING_DATA_MONGODB_URI: mongodb://localhost:32769/test
machine-service:
build: ./machine-service
ports:
- "2224:2224"
links:
- eureka
environment:
SPRING_APPLICATION_NAME: machine-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
Why I have exception opening a socket? How to fix this problem?
You are setting mongodb host in property file as localhost. In a container localhost address itself, but your mongodb is not in that container(car-service) which car-service run in. While you are using docker compose, you can address a container with its name. In your case it is mongodb.
to clarify #barbakini's answer, to define it in applications.yaml use:
spring.data.mongodb.host: mongodb
Your mongoDB service is not UP ,
Check the status by following command
sudo service mongodb status
sudo service mongodb start
Hope it should work, there could be several reasons as well ,
like the configuration you defined in your application for mongodb service is incorrect such as port .
Try to add
network_mode: host
and remove
links: ....
for all services, that want to connect with mongo or eureka or postgresql, inside your docker-compose.yml.
By doing so you will connect to docker localhost.