Arun Gupta
Arun Gupta

Reputation: 4035

Cross container communication with Docker

An application server is running as one Docker container and database running in another container. IP address of the database server is obtained as:

sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' db

Setting up JDBC resource in the application server to point to the database gives "java.net.ConnectException".

Linking containers is not an option since that only works on the same host.

How do I ensure that IP address of the database container is visible to the application server container?

Upvotes: 3

Views: 1826

Answers (5)

Arun Gupta
Arun Gupta

Reputation: 4035

Few things were missing that were not allowing the cross-container communication:

  • WildFly was not bound to 0.0.0.0 and thus was only accepting requests on eht0. This was fixed using "-b 0.0.0.0".
  • Firewall was not allowing the containers to communication. This was removed using "systemctl stop firewall; systemctl disable firewall"
  • Virtual Box image required a Host-only adapter

After this, the containers are able to communicate. Complete details are available at:

http://blog.arungupta.me/2014/12/wildfly-javaee7-mysql-link-two-docker-container-techtip65/

Upvotes: 0

errordeveloper
errordeveloper

Reputation: 6912

One simple way to solve this would be using Weave. It allows you to create many application-specific networks that can span multiple hosts as well as datacenters. It also has a very neat DNS-based service discovery mechanism.

I should disclaim, I am one of Weave engineering team.

Upvotes: 1

Usman Ismail
Usman Ismail

Reputation: 18649

If you want private networking between docker containers on remote hosts you can use weave to setup an overlay network between docker containers. If you don't need a private network just expose the ports using the -p switch and configure the addresses of the host machine as the destination IP in the required docker container.

Upvotes: 1

user2105103
user2105103

Reputation: 13085

The per host docker subnetwork is a Private Network. It's perhaps possible to have this address be routable, but it would be much pain. And it's further complicated because container IP's are not static.

What you need to do is publish the ports/services up to the host (via PORT in dockerfile and -p in your docker run) Then you just do host->host. You can resolve hosts by IP, Environment Variables, or good old DNS.

Upvotes: 0

Peter Lyons
Peter Lyons

Reputation: 145994

Linking containers is not an option since that only works on the same host.

So are you saying your application is a container running on docker server 1 and your db is a container on docker server 2? If so, you treat it like ordinary remote hosts. Your DB port needs to be exposed on docker server 2 and that IP:port needs to be configured into your application server, typically via environment variables.

Upvotes: 0

Related Questions