How to use variable in Marathon Docker parameters

2.3k views Asked by At

I'm using Marathon to run a Docker container, and am trying to set the DNS server for that container to point to a Consul agent on the local host. Inside the container, Marathon/Mesos is setting environment variables (which can be used in "args"), but I need the IP address of the slave to pass to the --dns option on the docker run. For example, this doesn't work, because the HOST variable can't be resolved:

{
    "id": "demo",
    "cmd": "python3 -m http.server 8080",
    "cpus": 0.5,
    "mem": 64.0,
    "instances": 2,
    "container": {
        "type": "DOCKER",
        "docker": {
            "image": "python:3",
            "network": "BRIDGE",
            "parameters": [{
                "key": "dns",
                "value": "$HOST"
            }],
            "portMappings": [{
                "containerPort": 8080,
                "hostPort": 0,
                "protocol": "tcp"
            }]
        }
    },
}

I've tried using an environment variable that the Mesos slave can access, but that doesn't seem to work either. Any suggestions?

1

There are 1 answers

1
Kalman Mocsar On BEST ANSWER

I have never tried to use the $HOST variable in the parameters list but you can use it in the cmd specifier. You can do something like this:

 "cmd": "python3 myscript.py $HOST",

You will get the hostname or the ip address of your mesos slave as the first program argument in myscript.py.

In myscript.py you need to do the folowings:

  1. Resolve the host name to ip address if the $HOST varaible contains host name.

    import socket
    ip = socket.gethostbyname(sys.argv[1])
    
  2. Change the nameserver in /etc/resolv.conf like this:

    with open('/etc/resolv.conf', 'w') as f:
        f.write('nameserver {}'.format(ip))