Search code examples
bashdockerawkdocker-composezabbix

Unique ID parameter in Docker-Compose


What I'm trying to achieve

When I'm launching docker-compose.yml I can launch a zabbix-agent container with a unique ID as a parameter.

What I've tried so far

I created the following config:

  zabbix:
    image: zabbix/zabbix-agent:alpine-trunk
    ports:
      - "10050:10050"
    privileged: true
    restart: unless-stopped
    environment:
      - ZBX_HOSTNAME="$MY_ARG" 
      - ZBX_SERVER_HOST="$MY_SERVER"

Now I'm trying to figure whether there's a way to launch a script to populate $MY_ARG internally.

A possible workaround

Simply launching docker-compose up inside a script where I can add arguments and read them internally as such:

…
# a script running before the docker-compose command:
export DIST=`grep -Po "^[A-Za-z]*" /etc/issue`
unique_id=$(case $DIST in
            Ubuntu)
               dmidecode -s baseboard-serial-number | tail -1
               ;;
            Raspbian)
               awk 'END{print $3}'  /proc/cpuinfo
               ;;
            *)
               echo None
               exit 1
        esac)

if awk -F= '/own/ && $2 != "" {err=1} END{exit err}' $ENV.env
then echo MY_ARG=$unique_id >> /etc/environment
# alternative way: export MY_ARG=$unique_id
fi

Any thoughts? Is this really the best way?


Solution

  • Got it! I just had to add export unique_id and use $unique_id in the docker-compose.yml file.

    Another problem I ran into was running docker-compose down and then docker compose up, so in order to do so I added the variable in front of the docker-compose command like this:

    unique_id=MY_ID docker-compose up
    

    Works like a charm!