1

I have my application that needs to serve on port :5000

Here's my dockerfile

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
ADD ./nginx.conf /etc/nginx/sites-available/default
RUN service nginx restart

RUN go get github.com/a/mycmd

EXPOSE 5000

And I run

sudo docker run --publish 5000:5000 --rm app /go/bin/mycmd

And here's my nginx config file:

limit_req_zone $binary_remote_addr zone=limit:10m rate=2r/s;

server {
    listen 80;

    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    server_name 123.13.13.13 example.com;

    location / {
        proxy_read_timeout 3000s;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:5000;
        limit_req zone=limit burst=5 nodelay;
    }
}

Then I expect this to redirect traffic from the webserver (port 80) to my app port 5000, but seems like nginx doesn't do the reverse proxy correctly. Traffic do not get directed to my app.

How do I set up nginx and my app in the same container so that I can use it as a reverse proxy?

Thanks!

1 Answers1

0

You mention in the nginx config:

proxy_pass http://127.0.0.1:8000;

But you want to redirect to the EXPOSE'd port 5000.

proxy_pass http://127.0.0.1:5000;

If it is a typo, then make sure you publish 80 to and host port (ot that you are using the container IP address directly.

Don't forget that, if you areusing a VM, you might have to port-forward that published port: see "Connect to a Service running inside a docker container from outside"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Sorry that was my typo. :( –  Jan 28 '16 at 10:34
  • @cvxv31431asdas OK, I have edited the answer. Are you using docker directly in a Linux host, or through a VM on Windows or Mac? – VonC Jan 28 '16 at 10:37
  • I am using Google cloud VM. Redirected port 80 to 5000. now works. Thought I didn't need that. Good to know! Thanks –  Jan 28 '16 at 10:40