Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx reverse proxy for Docker containers

I have a problem with reverse proxy to my Docker services. I have a local machine with IP 10.0.0.163 and with Docker stack running on it with nginx and portainer (for this question only they matter).

docker-compose.yml:

...
 portainer:
    image: portainer/portainer
    ports:
      - "9000:9000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/mnt/StorageDrive/Portainer:/data"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      ...
      - proxy

 reverse-proxy:
    image: reverseproxy:latest
    ports:
      - "80:80"
    networks:
      - proxy

networks:
  ...
  proxy:

nginx.conf:

worker_processes  1;  ## Default: 1

events { worker_connections  1024; }

http {

  sendfile     on;

  server {
    listen 80;
    allow all;

    location / {
      proxy_pass      http://10.0.0.163:9000;
    }
  }
}

Dockerfile for reverseproxy image:

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

When trying to access 10.0.0.163 I get error 502 and logs from reverseproxy show this:

2017/10/09 07:43:02 [error] 5#5: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 10.255.0.2, server: , request: "GET / HTTP/1.1", upstream: "http://10.0.0.163:9000/", host: "10.0.0.163"
10.255.0.2 - - [09/Oct/2017:07:43:02 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"

When typing 10.0.0.163:9000/ into browser - everything works fine. What is the problem? And how can I make it work with this URL

10.0.0.163/portainer/... -> 10.0.0.163:9000/...
like image 683
Igor Nehoroshev Avatar asked Oct 09 '17 08:10

Igor Nehoroshev


2 Answers

Try to change nginx configuration

  server {
    listen 80;
    allow all;

    location / {
      proxy_pass      http://portainer:9000/;
      resolver        127.0.0.11;
    }
  }

portainer is container name defined into your docker-compose.yml file 127.0.0.11 is embedded docker DNS server

Also. Alternative way. You can use jwilder/nginx-proxy instead of your reverse-proxy.

like image 151
Bukharov Sergey Avatar answered Sep 19 '22 17:09

Bukharov Sergey


The problem same me.

I resolve below,

# Docker run command
docker run --name portainer --network devops-net -d -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

# Nginx
location /portainer/ {
  proxy_http_version 1.1;
  proxy_set_header Connection "";
  proxy_pass http://portainer:9000/;
}

location /portainer/api/websocket/ {
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
  proxy_http_version 1.1;
  proxy_pass http://portainer:9000/api/websocket/;
}

# Notice
Portainer default port 9000

Open brower >> https://domain-name.dev/portainer

enter image description here

Read more: https://portainer.readthedocs.io/en/stable/faq.html

Work for me. ^____^

like image 38
bamossza Avatar answered Sep 18 '22 17:09

bamossza