0

I'm building a multi-container application with Docker. The full environment is on github should you wish to recreate it, but I include what I believe to be the relevant parts below for convenience. My nginx Dockerfile is like so:

FROM ubuntu:14.04
MAINTAINER Garry Cairns
ENV REFRESHED_AT 2015-02-11

# get the nginx package and set it up
RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "nginx"]

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
VOLUME ["/var/cache/nginx"]
EXPOSE 80 443

# load nginx conf
ADD ./site.conf /etc/nginx/sites-available/correspondence
RUN ["ln", "-s", "/etc/nginx/sites-available/correspondence", "/etc/nginx/sites-enabled/correspondence"]

CMD ["nginx", "-g", "daemon off;"]

And the site.conf file being added to sites-enabled looks like this:

# see http://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf#comment730384_577370
upstream api {
    server api_1:8000;
}

server {
    location / {
        proxy_pass       http://api;
    }
}

The nginx configuration there seems similar enough to that found in this answer that I feel the approach should be okay. But when I visit localhost (no port) on my machine or my domain in production I just get the nginx welcome page, no forwarding is going on.

I can connect to the running nginx container and wget correct results from the app container using http://api_1:8000 so I'm pretty sure the problem is in my nginx setup rather than my Docker one, but I can't puzzle this one out. Has anyone else solved this problem in the past?

Community
  • 1
  • 1
Garry Cairns
  • 3,005
  • 1
  • 18
  • 33
  • `listen 80, 443;` is syntax error. Where is `$API_PORT_8000_TCP_ADDR` defined? – Alexey Ten Feb 11 '15 at 13:01
  • @AlexeyTen yes I've changed that to just `listen 80;` while I'm debugging, will edit question accordingly. Fig/Docker automatically populate those environment variables when linking the containers. – Garry Cairns Feb 11 '15 at 13:06
  • Well, nginx does not use these variables in config so your config actually refers to empty variables. You could find usable this question http://serverfault.com/q/577370/211028 – Alexey Ten Feb 11 '15 at 13:12
  • @AlexeyTen thanks that looks promising. I should've thought of server fault but I suppose Docker kind of blurs those distinctions. I'll post the solution here if I get one. – Garry Cairns Feb 11 '15 at 13:18

1 Answers1

2

Finally figured this out. I needed to delete the default enabled site installed with nginx. My Dockerfile now reads:

FROM ubuntu:14.04
MAINTAINER Garry Cairns
ENV REFRESHED_AT 2015-02-11

# get the nginx package and set it up
RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "nginx"]

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
VOLUME ["/var/cache/nginx"]
EXPOSE 80 443

# load nginx conf
ADD ./site.conf /etc/nginx/sites-available/correspondence
RUN ["ln", "-s", "/etc/nginx/sites-available/correspondence", "/etc/nginx/sites-enabled/correspondence"]
RUN ["rm", "-rf", "/etc/nginx/sites-available/default"]
CMD ["nginx", "-g", "daemon off;"]

And all is well in the world.

Garry Cairns
  • 3,005
  • 1
  • 18
  • 33