Docker Swarm JDBC connection

Running a Postgresql DB on a Docker Swarm containing multiple nodes where the Database can be deployed. Using Docker version 1.12+.

Using a Data container, the Postgresql failover is working nicely. Now I would like to have a Java client connect to the DB and also survive failover. How should the JDBC connections be managed here? Does the connection string change? Or should it be managed through something like an nginx container running elsewhere? Is there an example of this available for study anywhere? Conceptually, I think I get moving this off to another (nginx-like) container, but can’t quite grok the details!

  • add external file to a dockerized spring boot web app inside tomcat
  • Why might gunicorn be hanging in this configuration?
  • How do i allocate enough ram to run Object.stringify without a memory / RangeError
  • Prevent docker-machine from downloading newer boot2docker iso?
  • Connection refused in Kubernetes
  • Migrating dockerized redis to another server
  • How to set up docker elasticsearch with node?
  • Docker Centos 7 easy_install supervisor ssl issue behind proxy
  • Postgres image from docker can't find initdb. What am I missing?
  • Unable to pull google/appengine-go docker image
  • multiple log_opts for docker logging
  • Error creating DigitalOcean droplet using docker-machine
  • One Solution collect form web for “Docker Swarm JDBC connection”

    In swarm mode, you get service discovery by DNS name for services in the same overlay network, you don’t need to add a proxy layer yourself. The swam networking docs go into detail, but in essence:

    docker network create -d overlay app-net
    docker service create --name app-db --network app-net [etc.]
    docker service create --name app-web --network app-net [etc.]

    Your database server is available by DNS within the network as app-db, to any service in the same app-net network. So app-db is the server name you use in your JDBC connection string. You can have multiple replicas of the Postgres container, or a single container which moves around at failover – the service will always be available at that address.

    But: I would be cautious about failover with your data container. You have a single container with your database state there; even if your state is in a volume, it won’t move around the cluster. So if the node with the data fails, your data container will start somwhere else, but the data won’t go with it.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.