How does Docker Swarm handle database (PostgreSQL) replication?

As collinp said, Docker swarm currently scales well for the stateless applications. For database replication, you have to rely on every database’s own replication mechanism. The volume or file system level replication could provide the protection for a single instance database, but are not aware of database replication/cluster.

For databases such as PostgreSQL, the additional works are required. There are a few options:

  1. Use host’s local directory. You will need to create one service for every replica, and use constraint to schedule the container to one specific host. You will also need custom postgresql docker image to set up the postgresql replication among replicas. While, when one node goes down, one PostgreSQL replica will go down. You will need to work to bring up another replica. See crunchydata’s example.

  2. Use the volume plugin, such as flocker, REX-Ray. You will still need to create one service for every replica, and bind one volume to one service. You need to create all services in the same overlay network and configure the PostgreSQL replicas to talk with each other via the dns name (the docker service name of the replica). You will still need to set up the postgresql replication among replicas.

  3. Use FireCamp. FireCamp is an open source project to simplify the setup and management of the stateful services, including PostgreSQL, on docker swarm. It does all the manual works for you. So you could setup a PostgreSQL cluster via one command.