Home > Blockchain >  docker - port 80 public to docker IP
docker - port 80 public to docker IP

Time:11-06

I am trying to point a domain name to my docker container. In our router we have put port 80 forwards to 192.168.1.101 (Server Docker is running on)

The container IP address however shows up like

"nextjs-docker-pm2-nginx-master-nextjs-1:172.18.0.2/16"
"nextjs-docker-pm2-nginx-master-nginx-1:172.18.0.4/16"
CONTAINER ID   IMAGE                                   COMMAND                  CREATED          STATUS          PORTS                                      NAMES
cabb3c8fe03c   nextjs-docker-pm2-nginx-master_nginx    "/docker-entrypoint.…"   16 minutes ago   Up 16 minutes   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nextjs-docker-pm2-nginx-master-nginx-1
ed27e0fd2f24   nextjs-docker-pm2-nginx-master_nextjs   "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   3000/tcp                                   nextjs-docker-pm2-nginx-master-nextjs-1

and my default.conf is

# Cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;

upstream nextjs {
  server nextjs:3000;
}


server {
    listen 80;

    server_name local.DOMAIN.com.au;
    server_tokens off;

    gzip on;
    gzip_proxied any;
    gzip_comp_level 4;
    gzip_types text/css application/javascript image/svg xml;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;


    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    # BUILT ASSETS (E.G. JS BUNDLES)
    # Browser cache - max cache headers from Next.js as build id in url
    # Server cache - valid forever (cleared after cache "inactive" period)
    location /_next/static {
      proxy_cache STATIC;
      proxy_pass http://nextjs;
    }

    # STATIC ASSETS (E.G. IMAGES)
    # Browser cache - "no-cache" headers from Next.js as no build id in url
    # Server cache - refresh regularly in case of changes
    location /static {
      proxy_cache STATIC;
      proxy_ignore_headers Cache-Control;
      proxy_cache_valid 60m;
      proxy_pass http://nextjs;
    }

    # DYNAMIC ASSETS - NO CACHE
    location / {
      proxy_pass http://nextjs;
      proxy_buffering off;
      proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 443 default_server ssl http2;

    server_name local.DOMAIN.com.au;

    ssl_certificate /etc/nginx/ssl/live/local.domain.com.au/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/local.domain.com.au/privkey.pem;
    
       location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    # BUILT ASSETS (E.G. JS BUNDLES)
    # Browser cache - max cache headers from Next.js as build id in url
    # Server cache - valid forever (cleared after cache "inactive" period)
    location /_next/static {
      proxy_cache STATIC;
      proxy_pass http://nextjs;
    }

    # STATIC ASSETS (E.G. IMAGES)
    # Browser cache - "no-cache" headers from Next.js as no build id in url
    # Server cache - refresh regularly in case of changes
    location /static {
      proxy_cache STATIC;
      proxy_ignore_headers Cache-Control;
      proxy_cache_valid 60m;
      proxy_pass http://nextjs;
    }

    # DYNAMIC ASSETS - NO CACHE
    location / {
      proxy_pass http://nextjs;
      proxy_buffering off;
      proxy_set_header X-Real-IP $remote_addr;
    }
}

our docker-compose file is

version: '3'
services:
  nextjs: 
    build: ./DRN1Git
  nginx: 
    user: $UID
    build: ./nginx
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./certbot/www:/var/www/certbot/:rw
      - ./certbot/conf/:/etc/nginx/ssl/:ro
  certbot:
    image: certbot/certbot:latest
    volumes:
      - ./certbot/www/:/var/www/certbot/:rw
      - ./certbot/conf/:/etc/letsencrypt/:rw

CodePudding user response:

By default Docker creates an internal network. (172.18.0.0/16 in your case). You need to map the port of the container to the outside of your docker host (192.168.1.101). See ports in compose for reference. E.g.:

version: "3.9"
services:
  web:
    build: nginx
      ports:
      - "80:80"

If you provide your docker-compose I will fit the example to suit your needs.

  • Related