Best practice for addon that requires 2 other docker images to run?

I am learning to develop addon using the HA docs and google searches of course. I am trying to create an addon (for Home Assistant Supervised).

My addon builds code from src and creates data files. However, to run, it requires two docker images from docker hub: mosquitto and engmqttclient. These are not HA addons but docker images so i somehow need to get these into my addon or somehow available and running so that my addon will work.

What is the correct/best way ensure these two images are built and run before the addon runs?

Dockerfile:

ARG BUILD_FROM
FROM $BUILD_FROM
#FROM hassioaddons
ENV LANG C.UTF-8
# Dependencies
RUN apk add docker
# Copy data for add-on
COPY rootfs /
# Run
RUN chmod a+x /usr/bin/run.sh
CMD [ "/usr/bin/run.sh" ]

run.sh

#!/usr/bin/with-contenv bashio
set -e
CONFIG_PATH=/data/options.json
HOST="$(bashio::config 'host')"
PORT="$(bashio::config 'port')"

__ADDON_EXIT_OK=0
__ADDON_EXIT_NOK_MOSQUITTO_BUILD=10
__ADDON_EXIT_NOK_ENGMQTT_BUILD=20
__ADDON_EXIT_NOK_ADDON_FAILURE=30

MOSQUITTO_OPTS=(
-d
-p 1883:1883
-p 8883:8883
-p 9001:9001
--network=host
)

ENGMQTT_OPTS=(
-d
--network=host
-e MQTT_BROKER="localhost"
)

function addon::info.config() {
  bashio::log.info "host=$HOST"
  bashio::log.info "port=$PORT"
  bashio::log.info "MOSQUITTO_OPTS=${MOSQUITTO_OPTS[@]}"
  bashio::log.info "ENGMQTT_OPTS=${ENGMQTT_OPTS[@]}"
  return ${__ADDON_EXIT_OK}
}

function addon::run.mosquitto() {
  bashio::log.info "Starting mosquitto MQTT broker.."
  if (docker run eclipse-mosquitto); then
    return ${__ADDON_EXIT_OK}
  fi
  return ${__ADDON_EXIT_NOK_MOSQUITTO_BUILD}
}

function addon::run.engmqttclient() {
  bashio::log.info "Starting EngMQTT client.."
  if (docker run gpbenton/engmqttclient:1.0.0); then
    return ${__ADDON_EXIT_OK}
  fi
  return ${__ADDON_EXIT_NOK_ENGMQTT_BUILD}
}

if (! addon::run.mosquitto); then
  bashio::log.fatal "Mosquitto build/run failed"
  exit ${__ADDON_EXIT_NOK_ADDON_FAILURE}
fi;
if (! addon::run.engmqttclient); then
  bashio::log.fatal "EngMQTTclient build/run failed"
  exit ${__ADDON_EXIT_NOK_ADDON_FAILURE}
fi

exit ${__ADDON_EXIT_OK}

I have made several attempts with this.

  • I tried using RUN apk add docker in the Dockerfile and then in my run,sh I included docker run … commands. But this failed during addon build, miserably, with lots of docker errors relating to permissions and sockets. Clearly this isn’t the right method.
  • I then remove the apk add docker and in my run.sh I included a apt-get install docker. This failed with apt-get not being available.

This is basic stuff and surely someone has done this and there is a best practice where an addon requires one+ docker images available on docker hub. I am probably doing something wrong here, particularly trying to run two docker images inside my addon which itself is essentially a docker image that is running within the HA environment - this is probably not the right way but I’m stuck on how else this can be implemented . Is it even possible? Is there some way my addon can stipulate that docker images are required - perhaps JSON config setting although I do not see how this can be stated in the addon’s config.json file for example (looking at the arguments list here https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config)

Anyone able to help please?

Hey, did you get anywhere with this? I need to use docker-compose, and it’s a pain but can be done.