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!

  • access my docker mysql container in java class
  • Docker image size
  • Docker WordPress Container Exits after ~30 seconds
  • docker interactive mode exits after ENTRYPOINT
  • How to manage code changes in rails with docker
  • Deploy AngularJS app into docker container
  • docker-compose build throwing error ERROR: Untar re-exec error: signal: killed: output:
  • In Dockerfile how to copy file from network drive
  • Access PostgreSQL running in Docker container in Vagrant
  • Using Kubernetes with an Artifactory/Nginx proxy
  • Why mysql backup is not restored into my docker container ?
  • Development environment and local sites URLs best practices
  • 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.