Home > Enterprise >  NGINX container as a proxy for other containers
NGINX container as a proxy for other containers

Time:02-07

I am trying to run of containers on my UBUNTU server, these containers are:

  1. DNS servers with bind9.
  2. NTP server with cturra/ntp.
  3. NGINX for reverse proxy => reverse proxy for DNS and NTP

I have these containers in the same yaml file:

version: '3'
services:
  reverse-proxy-engine:
    image: nginx
    container_name: reverse-proxy-engine
    volumes:
      - ~/core/reverse-proxy/:/usr/share/nginx/
    ports:
      - "80:80"
      - "443:443"
      - "53:53"
      - "123:123/udp"
    depends_on:
      - "DNS-SRV"
      - "ntp"
  DNS-SRV:
    container_name: DNS-SRV
    image: ubuntu/bind9
    user: root
    environment:
      - TZ=UTC
    volumes:
      - ~/core/bind9/:/etc/bind/
  ntp:
    image: cturra/ntp
    container_name: ntp
    restart: always
    read_only: true
    tmpfs:
      - /etc/chrony:rw,mode=1750
      - /run/chrony:rw,mode=1750
      - /var/lib/chrony:rw,mode=1750
    environment:
      - NTP_SERVERS=time.cloudflare.com
      - LOG_LEVEL=0

After running this yaml file, the containers are created and I see the ports mapped correctly:

admin@main-srv:~/core/yamls$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS                                                                                NAMES
4720bae2a44c        nginx               "/docker-entrypoint.…"   5 seconds ago       Up 4 seconds                      0.0.0.0:53->53/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:123->123/udp   reverse-proxy-engine
1681814f651e        cturra/ntp          "/bin/sh /opt/startu…"   6 seconds ago       Up 5 seconds (health: starting)   123/udp                                                                              ntp
dde2f9094b45        ubuntu/bind9        "docker-entrypoint.sh"   6 seconds ago       Up 5 seconds                      53/tcp                                                                               DNS-SRV

I am able to access the nginx webpage on the browser using port 80 with <UBUNTU_SERVER_IP:80>, but I'm unable to use this same IP to resolve DNS or NTP on the same network, but within the containers network, it's working.

So I think that NGINX ports are exposed to the UBUNTU server, but the DNS and NTP ports are not exposed to NGINX, would that be correct? What am I missing?

Below is my NginX configuration file:

events {
  worker_connections 1024;
}

stream {

    upstream dns_servers {
        server DNS-SRV:53;
    }

    upstream ntp_server {
      server ntp:123;
    }

    server {
        listen 53 udp;
        listen 53; #tcp
        proxy_pass dns_servers;
        error_log  /var/log/nginx/dns.log info;
        proxy_responses 1;
        proxy_timeout   1s;
    }

    server {
      listen 123 udp;
      listen 123; #tcp

      proxy_pass ntp_server;
      error_log  /var/log/nginx/ntp.log info;
      proxy_responses 1;
      proxy_timeout   1s;
    }
}

So far it seems logical to me, any ideas?

CodePudding user response:

I think that's because you don't set hostname for bind and ntp container, I use below configuration and get it working

version: '3'
services:
  reverse-proxy-engine:
    image: nginx
    container_name: reverse-proxy-engine
    volumes:
      - ~/core/reverse-proxy/:/usr/share/nginx/
      - $PWD/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - "80:80"
      - "443:443"
      - "53:53"
      - "123:123/udp"
    depends_on:
      - "DNS-SRV"
      - "ntp"
  DNS-SRV:
    container_name: DNS-SRV
    hostname: DNS-SRV
    image: ubuntu/bind9
    user: root
    environment:
      - TZ=UTC
    volumes:
      - ~/core/bind9/:/etc/bind/
  ntp:
    image: cturra/ntp
    container_name: ntp
    hostname: ntp
    restart: always
    read_only: true
    tmpfs:
      - /etc/chrony:rw,mode=1750
      - /run/chrony:rw,mode=1750
      - /var/lib/chrony:rw,mode=1750
    environment:
      - NTP_SERVERS=time.cloudflare.com
      - LOG_LEVEL=0

In above configuration I add hostname for bind and ntp container, also i mount nginx configuration and replace the default configuration.

Below nginx.conf configuration

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

stream {

    upstream dns_servers {
        server DNS-SRV:53;
    }

    upstream ntp_server {
      server ntp:123;
    }

    server {
        listen 53 udp;
        listen 53; #tcp
        proxy_pass dns_servers;
        error_log  /var/log/nginx/dns.log info;
        proxy_responses 1;
        proxy_timeout   1s;
    }

    server {
      listen 123 udp;
      listen 123; #tcp

      proxy_pass ntp_server;
      error_log  /var/log/nginx/ntp.log info;
      proxy_responses 1;
      proxy_timeout   1s;
    }
}

Note: Make sure the binding port you use 80, 443, 53, 123 not used by other application.

  •  Tags:  
  • Related