Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Docker Nginx on a non-docker host application

I have many docker containers which pass through docker nginx combo (docker-compose.yml outlined below) and they work very well. I want docker nginx to do the same to a non-docker app thats running on localhost:8080, that is I want docker nginx container to run connections to example.com to 127.0.0.1:8080 where 127.0.0.1:8080 is ran by a non-docker app (code-server do be specific but that shouldn't matter)

version: '3'

services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /apps/proxy/conf:/etc/nginx/conf.d
      - /apps/proxy/vhost:/etc/nginx/vhost.d
      - /apps/proxy/html:/usr/share/nginx/html
      - /apps/proxy/dhparam:/etc/nginx/dhparam
      - /apps/proxy/certs:/etc/nginx/certs:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: always

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nginx-proxy-le
    depends_on:
      - nginx-proxy
    volumes:
      - /apps/proxy/vhost:/etc/nginx/vhost.d
      - /apps/proxy/html:/usr/share/nginx/html
      - /apps/proxy/dhparam:/etc/nginx/dhparam:ro
      - /apps/proxy/certs:/etc/nginx/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      - NGINX_PROXY_CONTAINER=nginx-proxy
    restart: always

networks:
  default:
    external:
      name: nginx-proxy

and its running well on docker containers, the moment i include "nginx-proxy" so that it can detect them, fantastic tool. I cant simply paste something like this into default.conf (conf of nginx )

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    location / {
      proxy_pass http://localhost:8080/;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection upgrade;
      proxy_set_header Accept-Encoding gzip;
    }
}

My guess is that what is "localhost" for me is not "localhost" for nginx given that it is inside a docker (im new to this docker stuff, so I might be talking shit). I saw the issue where they mentioned many approaches, none of them worked for me. In particular I tried running a dummy docker as CWempe suggested

docker run -d 
    -e VIRTUAL_HOST=foo.bar.com 
    -e VIRTUAL_PORT=8080 
    -e UPSTREAM_NAME=webserver.local 
    --rm 
    cwempe/docker-dummy:latest

This didnt't work, nginx didnt even detect it which made me think its probably because its not on the nginx-network so i added that (and turned it into docker-compose for convenience)

version: '3.3'
services:
    docker-dummy:
        environment:
            - VIRTUAL_HOST=example.com
            - VIRTUAL_PORT=8080
            - UPSTREAM_NAME=127.0.0.1
        image: 'cwempe/docker-dummy:latest'

networks:
  default:
    external:
      name: nginx-proxy

Then looking at default.conf i get

# mydomain.com
upstream mydomain.com {
                                ## Can be connected with "nginx-proxy" network
                # code-server_docker-dummy_1
                        server 172.25.0.7 down;
}
server {
        server_name mydomain.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        include /etc/nginx/vhost.d/default;
        location / {
                proxy_pass http://example.com;
        }
}
server {
        server_name example.com;
        listen 443 ssl http2 ;
        access_log /var/log/nginx/access.log vhost;
        return 500;
        ssl_certificate /etc/nginx/certs/default.crt;
        ssl_certificate_key /etc/nginx/certs/default.key;
}

So sure it has seen it but it also believes it is down and doesn't include the VIRTUAL_PORT at all and obviously the 127.25.0.7 IP doesn't make sense to me either. Changing 127.25.0.7 -> 127.0.0.1:8080 does nothing. Any idea how I can remedy this ? Thank you for your input in advance.

question from:https://stackoverflow.com/questions/65885398/docker-nginx-on-a-non-docker-host-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...