1

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?

Yaron
  • 1,199
  • 1
  • 15
  • 35
  • 1
    There is a [similar question](https://stackoverflow.com/questions/43208979/docker-run-script-in-host-on-docker-compose-up), but without a good answer. My suggestion would be to create your own Docker image (based on the original image), where you set the environment variable. The idea of containers is that they run independently from the host environment, so having some kind of pre/post script run on the host does not fit well into the Docker philosophy. – user3151902 Sep 15 '17 at 06:23
  • So using an environment variable is not so bad after all :), thanks! – Yaron Sep 15 '17 at 06:46
  • I'm thinking about implementing an ID using dmidecode (or /proc/cpuinfo in RPi) as a hostname, I'll let you know how it all went. – Yaron Sep 17 '17 at 07:26

1 Answers1

0

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!

Yaron
  • 1,199
  • 1
  • 15
  • 35