Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use environment vars of container in command key of docker-compose

I have two services in my docker-compose.yml: docker-gen and nginx. Docker-gen is linked to nginx. In order for docker-gen to work I must pass the actual name or hash of nginx container so that docker-gen can restart nginx on change.

When I link docker-gen to nginx, a set of environment variables appears in the docker-gen container, the most interesting to me is NGINX_NAME – it's the name of nginx container.

So it should be straightforward to put $NGINX_NAME in command field of service and get it to work. But $NGINX_NAME doesn't expand when I start the services. Looking through docker-gen logs I see the lines:

2015/04/24 12:54:27 Sending container '$NGINX_NAME' signal '1'
2015/04/24 12:54:27 Error sending signal to container: No such container: $NGINX_NAME

My docker_config.yml is as follows:

nginx:
  image: nginx:latest
  ports:
    - '80:80'
  volumes:
    - /tmp/nginx:/etc/nginx/conf.d

dockergen:
  image: jwilder/docker-gen:latest
  links:
    - nginx
  volumes_from:
    - nginx
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock
    - ./extra:/etc/docker-gen/templates
    - /etc/nginx/certs
  tty: true
  command: >
    -watch
    -only-exposed
    -notify-sighup "$NGINX_NAME"
    /etc/docker-gen/templates/nginx.tmpl
    /etc/nginx/conf.d/default.conf

Is there a way to put environment variable placeholder in command so it could expand to actual value when the container is up?

like image 566
Perlence Avatar asked Sep 29 '22 04:09

Perlence


1 Answers

I've added entrypoint setting to dockergen service and changed command a bit:

dockergen:
  image: jwilder/docker-gen:latest
  links:
    - nginx
  volumes_from:
    - nginx
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock
    - ./extra:/etc/docker-gen/templates
    - /etc/nginx/certs
  tty: true
  entrypoint: ["/bin/sh", "-c"]
  command: >
    "
    docker-gen
    -watch
    -only-exposed
    -notify-sighup $(echo $NGINX_NAME | tail -c +2)
    /etc/docker-gen/templates/nginx.tmpl
    /etc/nginx/conf.d/default.conf
    "

Container names injected by Docker linking start with '/', but when I send SIGHUP to containers with leading slash, the signal doesn't arrive:

$ docker kill -s SIGHUP /myproject_dockergen_1/nginx

If I strip it though, nginx restarts as it should. So this $(echo $NGINX_NAME | tail -c +2) part is here to remove first char from $NGINX_NAME.

like image 190
Perlence Avatar answered Oct 07 '22 21:10

Perlence