5

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?

3
  • Is the env variable not getting parsed and just being passed as a string? Try taking the double quotes off and post back the message. Commented May 3, 2015 at 1:35
  • Thanks for reply. If I remove double quotes I get the same message. Same goes for ${NGINX_NAME} Commented May 5, 2015 at 6:32
  • I think that environment variables are not substituted because the entrypoint of docker-gen is /usr/local/bin/docker-gen and not /bin/sh -c. It simply doesn't know what to do with variables. Commented May 5, 2015 at 9:26

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for this. You set me on the right track. I had completely missed the "unable to reststart nginx" message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.