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!

  • Services in CentOS 7 Docker image without systemd
  • AWS Elastic Beanstalk Docker PHP application
  • docker swarm stop containers on quorum loss
  • Can't get clipboard forwarding working, while being in Docker under SSH
  • Issue when joining serf nodes located in different Docker containers
  • nginx reverse proxy for docker service
  • Gitlab Docker container behind reverse Proxy
  • Pass parameter (like“TestNode”) in docker run from Ansible or Fig
  • Docker - Selenium Grid -> container with windows and FF or with mac os and safari
  • How exactly does docker work? (Theory)
  • Dockerfile: Docker build can't download packages: centos->yum, debian/ubuntu->apt-get behind intranet
  • How to sandbox Ansible playbooks for high availability Glassfish application on AWS
  • 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.