Home > Net >  How to run single Django project on multiple static IP address in docker
How to run single Django project on multiple static IP address in docker

Time:08-13

Here, this project works fine. This project is opening only on single static IP address.

The problem is I want to open this project on multiple static IP address in docker. Can anyone tell me how to do this?

This help will be good for me.

Please help.

docker-compose.yml:

version: '3.9'

services:
  app:
    build:
      context: .
      args:
       - DEV=true
    ports:
      - '8000:8000'
    volumes:
      - ./app:/app
      - dev-static-data:/vol/web
    command: >
      sh -c 'python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000'
    environment:
      - DB_HOST=db
      - DB_NAME=devdb
      - DB_USER=postgres
      - DB_PASS=m@noj5078
      - DEBUG=1
    depends_on:
      - db
  db:
    image: postgres:13-alpine
    volumes:
      - dev-db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=devdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=m@noj5078
volumes:
  dev-db-data:
  dev-static-data:

proxy/default.conf.tpl

server {
    listen ${LISTEN_PORT};

    location /static {
        alias /vol/static;
    }

    location / {
        uwsgi_pass           ${APP_HOST}:${APP_PORT};
        include              /etc/nginx/uwsgi_params;
        client_max_body_size 10M;
    }
}

CodePudding user response:

Assign multiple IP addresses to your host system in the normal way. This will involve working with your network administrator to get those addresses and OS-specific setup to assign them to your system; this is out of scope for a Stack Overflow question.

The Compose ports: will then listen on all of the host interfaces. You don't need to change anything inside the containers.

If the system has multiple interfaces but you only want to listen on some of them, ports: takes an optional IP-address parameter that specifies a specific host interface to use. You can listen on all, some, or no interfaces, and you can specify a different host port on each address if you'd like.

ports:
  # external interface: listen to default HTTP port 80
  - '10.10.10.10:80:8000'

  # internal interface: listen to "native" port 8000
  - '10.20.20.20:8000:8000'

  # localhost-only: listen to "native" port as well
  - '127.0.0.1:8000:8000'

  # ignore third interface 10.30.30.30, this container
  # will not be accessible there
  • Related