Override with an inline entry point on docker-compose.yml

This simple post I will demonstrate how to override an entry point with an inline script to any docker-compose.yml.

According to docker compose reference, the default entry point of an image could be overridden, pointing to a file inside the image or defining it inline.

For example, this stack definition defines a CMAK service (administrative UI for Kafka clusters) is based in an image that does not support the usage of _FILE environment variables to use docker secrets, so we define a new multiline entry point with a command (/bin/bash) & a list of arguments, last argument is a pipeline | to indicate YAML file that we want to define several lines (all must be well indented).

Any dollar $ inside the script must be escaped using double dollar $$ to avoid YAML variable replacements.

The docker entry point is based on MariaDB entry point script to parse _FILE variables if present to read its content. Last line of the script is just calling the default CMAK entry point bin/cmak.

version: '3.8'
services:
  cmak:
    image: iunera/cmak:3.0.0.5
    secrets:
      - cmak_password
    environment:
      ZK_HOSTS: zookeeper_1:2181,zookeeper_2:2181,zookeeper_3:2181
      KAFKA_MANAGER_AUTH_ENABLED: 'true'
      KAFKA_MANAGER_USERNAME: admin
      KAFKA_MANAGER_PASSWORD_FILE: /run/secrets/cmak_password
    entrypoint:
      - /bin/bash
      - -c
      - |
        set -eo pipefail
        function file_env {
          local var="$$1"
          local fileVar="$${var}_FILE"
          local def="$${2:-}"
          if [ "$${!var:-}" ] && [ "$${!fileVar:-}" ]; then
            echo "Both $$var and $$fileVar are set, but are exclusive"
            exit 1
          fi
          local val="$$def"
          if [ "$${!var:-}" ]; then
            echo "$$var was defined through environment variable"
            val="$${!var}"
          elif [ "$${!fileVar:-}" ]; then
            echo "$$var was defined through file environment variable $$fileVar"
            val="$$(< "$${!fileVar}")"
          fi
          export "$$var"="$$val"
          unset "$$fileVar"
        }
        file_env 'KAFKA_MANAGER_USERNAME'
        file_env 'KAFKA_MANAGER_PASSWORD'
        bin/cmak
secrets:
  cmak_password:
    external: true
    name: kafka_cmak_password_v1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.