I have a docker swarm setup with services registered in an overlay network. Comunication between services is working fine however I get a "Connection refused" from one of my services that connects to an external DB. The service is a Java based application (spring boot).
The connection is defined with its IP (jdbc:mysql://192.168.130.141:3306/database?autoReconnect=true)
I have checked that I can ping the server from the container docker exec -it e093 ping 192.168.130.141 gives expected answer
I have rechecked (4 times) my credentials
I can connect to the DB from the host
Host is Ubuntu 16.04.4 LTS
I am running docker 18.03.0-ce and the exact error is
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_151]
I'll skip the full stack trace here.
Any help would be greatly appreciated
--EDIT--
Let me clarify that the database is a Galera cluster and the IP I try to connect to is the load balancer (HAProxy) in front of it. I know the setup works as I have other applications connecting to the cluster in this matter as well as the service itself (when not in a docker container).
So, as far as I can tell, my connection informations are correct but it doesn't connect from inside docker services.
Can you show your docker-compose.yml? In the ports you have to expose the port that you need to access.
ports:
- 3306:3306
You can install telnet to test if the port is open using this commands inside the container
docker container ls
docker exec -it conainerid bash
apt-get update
apt-get install telnet
Related
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 2 years ago.
I have a Spring app that connects to local Postgres db. It works fine.
I then created a Docker image from the Spring app. When running the container it throws a db connection error.
I have followed the solution suggested here, but still getting the same error.
[ERROR ] [task-1] c.z.h.p.HikariPool c.z.h.p.HikariPool.throwPoolInitializationException(HikariPool.java:593) – HikariPool-1 - Exception during pool initialization.
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.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
at org.postgresql.Driver.makeConnection(Driver.java:458)
at org.postgresql.Driver.connect(Driver.java:260)
I have run the docker image in several ways with the same result.
docker run app-image
docker run --network=host app-image
Dockerfile:
FROM java:8-jdk-alpine
COPY ./build/libs/runtime-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "runtime-0.0.1-SNAPSHOT.jar"]
# Expose standard tomcat port
EXPOSE 9888
postgresql.conf:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*'
pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
host postgres postgres 0.0.0.0/0 trust
host postgres postgres 172.17.0.0/16 trust
The spring application tries to connect to localhost:5432. Since the host and each container are separate network entities (i.e., the host and each container has its own network address in the docker-internal network), the container tries to connect to the database running on it. Since there is not database running, the connection cannot be established.
To fix this issue, you need to get the IP address of the host machine within the docker-internal network (see, e.g., this article at devilbox) and use this address as database server for the spring application.
I would recommend to run the postgres in a docker container aswell. You can, for example, use the official postgres image from dockerhub. Then you can reference the postgres-container from within the spring-container by the postgres-container's name.
I have reserved a number of Linux based EC2 machines(running OS : Centos 7) and with the exact same versions of JMeter 4.0 and Java installed.
No active firewalls, all the devices are connected within the same subnet and I can easily ping between them.
The problem is that I when I try to configure a mater machine and connect it to one of my slaves I keep receiving this exception
Connection refused to host: xx.yy.zz.4; nested exception is:
java.net.ConnectException: Connection timed out (Connection timed out)
Failed to configure xx.yy.zz.4
Stopping remote engines
Remote engines have been stopped
Error in NonGUIDriver java.lang.RuntimeException: Following remote engines could not be configured:[xx.yy.zz.4]
I have made my slave(ip: xx.yy.zz.4) setup as follows:
server_port = 24000 , server.rmi.localport=26000 , server.rmi.ssl.disable=true, No Remote Hosts and I usually launch the server using this shell command:
./jmeter-server -n -Djava.rmi.server.hostname=xx.yy.zz.4
And for the master which is also another EC2 instance(ip: xx.yy.zz.21) I made these configurations:
remote_hosts=xx.yy.zz.4:24000 , client.rmi.localport=25000, server.rmi.ssl.disable=true
And I use this shell command to start my JMeter's test plan
./jmeter -n -Rxx.yy.zz.4 -t /home/centos/jmeter/scripts/demo.jmx -Djava.rmi.server.hostname=xx.yy.zz.21
Any idea on what might cause these error?
Minimal configuration:
slave:
./jmeter-server -Dserver_port=24000
master:
./jmeter -n -R xx.yy.zz.4:24000 -t test.jmx
More information:
How to Perform Distributed Testing in JMeter
Remote hosts and RMI configuration
Also be aware that according to JMeter Best Practices you should be always using the latest JMeter version so consider upgrading to JMeter 5.2.1 (or whatever is the latest stable version available at JMeter Downloads page) on next available opportunity
I am trying to run some integration tests in my Jenkins pipeline connecting to a mysql docker container. For some reason, we can't connect to it. We have checked that the credentials are right and that the port is exposed to the host:
mysql:5.6 0.0.0.0:3360->3306/tcp mysql
Our Jenkins runs in a host, it isn't a docker container.
I have inspect the container, and everything is the same as in my local environment, where I can connect to the container. This is the error I am getting:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Any idea what it could be?
Thanks in advance.
I have a problem with connecting to existing remote MSSQL database from inside of a docker container running in stack.
My application consists of three modules (backend, frontend and haproxy)
Backend module is written in Java (SpringBoot app) and it's also the one that needs to connect do remote MSSQL database (by remote I mean placed on different sever, separate of docker part).
I have the following docker compose file:
I start the stack by using following command:
docker stack deploy -c docker-compose.yml myapp
The result is, all containers are up and running, but spring app reports that connection to DB is timed out:
Server seems to be configured properly, I am able to access the host from container through telnet.
When running independently (even from docker container) backend app is able to connect to database with no problems, while stacked with docker-compose however it's unable to connect to the very same db.
I've also tried to provide db server IP instead of host name - no success.
Maybe setting up networks section in docker compose would do the trick?
UPDATE
Another thing you can do is to use host.docker.internal instead of the IP address of the database. This ONLY works on docker for windows or docker for mac.
Source: I want to connect from a container to a service on the host
OLD, only works when not in swarm mode
You need to specify that docker should use the same network as the host, you can do this in the following way:
version: '3'
services:
web-app:
build:
dockerfile: web-app/something
ports:
- 8080:8080
network_mode: "host"
Reference: Use host networking
It seems that the issue was caused by networks overlapping.
Adding network configured as show below, allowed either to connect to my remote database and keep my endpoints hidden:
networks:
backend:
ipam:
driver: default
config:
- subnet: 192.168.40.0/26
So I have a clone of the java application, and an active couchbase database, everything on my local machine. When I run the java spring application, everything works well, the application will connect to the database.
On the other hand, I use the same project and database configurations using docker containers. But I can't seem to make them communicate, and when the app starts, I keep getting this exception:
java.net.ConnectException: Connection refused: localhost/127.0.0.1:8091
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at com.couchbase.client.deps.io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:223)
at com.couchbase.client.deps.io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:285)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:589)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:513)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:427)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:399)
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140)
at com.couchbase.client.deps.io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
at java.lang.Thread.run(Thread.java:745)
I also created a docker network where I put the app and database.
Any idea why there is no connectivity ?
you try to connect to container's localhost
java.net.ConnectException: Connection refused:
localhost/127.0.0.1:8091
But you need to connect to another container. If you run docker container with argument --link mysql:mysql you need to specify into app configuration file connect to mysql instead of localhost of 127.0.0.1