Skip to content

Using Docker compose for more advanced use cases

Deploying a simple "Hello World" container with docker-composeΒΆ

Deploying a simple "Hello World" container with docker-compose

In this tutorial we show you how to install Docker and setup the famous "Hello World" container with docker-compose using qbee.io's powerful configuration management. Here is a Docker tutorial, if you only need basic Docker.

In order to deploy a Docker container with docker-compose we need two things:

  • Install Docker: Make sure Docker (and docker compose) is installed on the system
  • Deploy the container: create a docker-compose.yml file that defines your container

The difference between docker-compose and docker compose

Compose from V2 integrates compose functions into the Docker platform, continuing to support most of the previous docker-compose features and flags. You can run Compose V2 and later by replacing the hyphen (-) with a space, using docker compose, instead of docker-compose. So please adjust for your version of Docker. We use V3 for this tutorial.

In order to install and deploy the docker container we use file distribution. As a first step we create a script that will install Docker on the target device. For this docker-compose demo we use a Raspberry Pi running Raspbian Buster which is a Debian 10 based system. However, this should work on basically any Linux.

install-docker-compose.sh

#! usr/bin/bash
#Install docker-compose on Debian 10

curl -fsSL https://get.docker.com -o get-docker.sh

sudo bash get-docker.sh

docker compose version

This script is uploaded into the qbee file manager, and then we use file distribution to distribute and run it. We execute it with the bash /home/pi/install-docker-compose.sh command to run.

!docker-compose-file-set1

In addition, we define a second file distribution that will distribute our docker-compose.yml file containing the information for Docker compose.

docker-compose.yml

version: '3'
services:
    hello_world:
        image: ubuntu:latest
        command: [/bin/echo, '"Hello World"']

!docker-compose-file-set2

When this is deployed Docker will be installed and the Docker compose file will be executed. This starts the "Hello World" container which basically just outputs the text from above. Please see how qbee is reporting this. Our log functionality allows you even to see the output of the container run as well as the initial installation returning the installed version of docker-compose.

!log-screen-showing-docker-compose-output

This is a very simple example of using docker compose but it allows you great flexibility. Let's move to templated docker-compose.yml which give you extreme power and flexibility.

Using templating to create flexible docker-compose filesΒΆ

Learn how to individualize docker-compose.yml files with templating

In qbee we use a Mustache based key-value templating engine that enables you to expose any part of a script or file in order to access it through the qbee UI or the API. This gives you powerful options to individualize containers based on group definitions or even individual definitions.

Building on the previous example we need again a script that makes sure that Docker is installed. But the docker-compose.yml file is now changed into a templated version. To tell them apart we name it docker-compose.yml.tmpl. This file is used to make the important parts accessible as key value pairs. Any key is defined with the Mustache curly brackets such as {{key}}.

docker-compose.yml.tmpl

version: '3'
services:
    hello_world:
        image: {{version}}
        command: [/bin/echo, '{{text}}']

Now we need to upload this to the file manager and make sure that we move it onto the device as docker-compose.yml. In addition, we need to define the key-value pairs in the UI.

!docker-compose-with-templating

The full configuration json can be found here:

file distribution configuration as json
{
"enabled": true,
"version": "v1",
"files": [
  {
    "templates": [
      {
        "source": "/install-docker-compose.sh",
        "destination": "/home/pi/install-docker-compose.sh",
        "is_template": false
       }
      ],
     "command": "bash /home/pi/install-docker-compose.sh"
   },
   {
     "templates": [
       {
          "source": "/docker-compose.yml.tmpl",
         "destination": "/home/pi/docker-compose/docker-compose.yml",
         "is_template": true
      }
    ],
    "parameters": [
      {
        "key": "version",
        "value": "ubuntu:latest"
      },
      {
        "key": "text",
        "value": "This is a new text introduced as key-value pair via templating"
      }
    ],
    "command": "cd /home/pi/docker-compose/ &&\nsudo docker compose up"
  }
 ]
}

This result in the following output being reported back through the qbee logging functionality. Please see how the text is changed according to the key-value input. Any changed key value input will now re-run the docker-compose.

!log-screen-with-templated-docker-compose-output

Advanced setups with docker-composeΒΆ

This was a very simple example that is not really useful. But please consider that you can run complex scripts in "command to run". It is also possible to manage multiple containers with this in a very elegant way. If you want to learn more about how to do this we recommend this tutorial how to setup a system with Node-RED, InfluxDB and Grafana.