For many applications, a database of some sort is required. A big question when packaging an on-prem app is whether to embed this database alongside the rest of your application, or to require an end customer to provide the connection details for a DB instance that they manage.

Tradeoffs

The key tradeoff is ease-of-setup for the customer vs. ease-of-maintainability for the application vendor, and often the answer is to allow both configurations. You can include an embedded database for trials and proof-of-concept installations, and then when your customer wants to set up a hardened production installation, they can supply connection details.

Docker Swarm Postgres Example

Configuration

First, we need some configuration items to allow the end customer to configure which mode they want to run in

Docker Swarm

On the swarm side, we need to reference those config options to control what runs in the cluster. We’ll toggle the number of postgres replicas that run, as well as the connection details for our example API server.

Note that this example omits (for brevity) how to securely create a persistent password/username for your postgres instance. A good guide for this can be found in the Populating Config Values with Commands