Kontena stacks were created for combining your application and the components it needs into a single unit. The Kontena 1.4 release improves the reusability and replaceability of those components by introducing stack dependencies.

Your applications often need a database or other components to run. Adding a database as a service to your application's stack file gets you started nicely.

Of course, you don't want to run a production database without some kind of high availability clustering.

You can improve your stack's scalability by configuring a database master service, adding a scalable database node service and defining pre-start deploy hooks for instance registration and initial credential configuration. But perhaps you're not a database wizard or don't really want to spend a lot of time setting it up.

Another option is to leverage the power of the community and install a preconfigured clustered high availability database stack, such as kontena/cockroachdb. You can install it using kontena stack install kontena/cockroachdb, link to it from your application stack and be done with it. Nice and simple!

However, there is a problem. Your application stack can't be deployed unless the target environment has a correctly named instance of the database stack installed and it has all the necessary databases and credentials in place.

The new and better third option is to use the new stack dependencies feature that was added in Kontena 1.4 release.

This is how it works

There's a new Kontena Stack YAML top level key depends: that you can use to list additional stacks that need to be installed together with your stack and this is what a stack using it looks like:

stack: example/app
version: 0.1.0
expose: app
    stack: kontena/cockroachdb
    image: example/app
      - "DB_HOST=${cockroachdb}" # becomes "DB_HOST=app-cockroachdb"

Now you can install the application together with the database by running a single command:

$ kontena stack install
> Installing dependency cochroachdb as app-cockroachdb ..

When you remove the application stack, the database is removed together with it:

$ kontena stack remove --force app
> Removing dependency app-cochroachdb ..

And the same goes with upgrade:

$ kontena stack upgrade app 
 [done] Reading stack app from master     
 [done] Parsing kontena.yml     
> Analyzing upgrade ..
These services will be upgraded:
- app-cockroachdb/db
- app/app

These stacks will be upgraded and deployed:
- app-cockroachrb
- app

Passing variables to dependencies

Most stacks have several options for configuration and customization and often your application or personal preferences dictate what selections should be made during the installation.

For example, let's say you've decided to use a ready-made redis-stack as a dependency. Great, but it keeps asking you to select a redis version every time you install it. Someone installing your stack could make a wrong choice and select something that isn't compatible with the application.

To avoid that, you can supply values to child stack variables from the parent stack using the variables keyword:

stack: example/app
version: 0.1.0
expose: app
    stack: kontena/redis
      version: 3.2-alpine
    image: example/app

Now you won't get prompted and always get the 3.2-alpine variant of redis.

Passing variables to dependencies using command-line options

Kontena 1.4 also adds the option to set variable values through the command-line using -v variable=value syntax. You can supply values for the dependency stack variables by using dot notation:

$ kontena stack install -v redis.version=3.2-alpine
> Installing dependency redis as app-redis ..
[DONE] Creating stack app 


You can use stack dependencies to break a stack monolith into bite-sized reusable micro stacks. Now where did I hear that before? Oh, right.

Image: Davis Staedtler

About Kontena

Kontena provides the most easy-to-use, fully integrated solution for DevOps and software development teams to deploy, run, monitor and operate containers on the cloud. The underlying Kontena Platform technology is open source and available under Apache 2.0 license. It is used by hundreds of startups and software development teams working for some of the biggest enterprises in the world. www.kontena.io