0

I'm trying to run Nginx and PHP-FPM containers with network_mode: host in docker-compose, when defining a network_mode as "bridge" it works fine, but when defining network_mode as "host" I receive :

nginx: [emerg] host not found in upstream "ui" in /etc/nginx/conf.d/ui.conf:10

Everything works fine when setting network_mode to bridge, unfortunately I need to set it to host, as I need access to host network so I can access ueye camera.

This is my docker-compose file

version: '3'

services:
 nginx:
    image: nginx:alpine
    container_name: nginx
    tty: true
    ports:
      - "80:80"
      - "443:443"
      - "8000:8000"
    volumes:
      - ./../../ui/:/var/www/
      - ./nginx/:/etc/nginx/conf.d/
    network_mode: host

    depends_on:
      - ui
    restart: always
 ui:
    build:
      context: ./../../ui
      dockerfile: Dockerfile
    volumes:
      - ./../../ui/:/var/www/
    container_name: ui
    tty: true
    environment:
      SERVICE_NAME: ui
    working_dir: /var/www
    network_mode: host
    ports:
      - "9000:9000"

The nginx conf file

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass ui:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}
Nourdine
  • 67
  • 1
  • 9
  • I changed fastcgi_pass to **localhost:9000;** as @vivekyad4v suggested, now curl command return the page, but browser access still not working! – Nourdine Jun 21 '19 at 02:45

1 Answers1

0

While using host network mode, your container will see Docker host as localhost & not the container itself because it's using the host network. Try replacing ui with localhost in your nginx conf -

    fastcgi_pass localhost:9000;
vivekyad4v
  • 13,321
  • 4
  • 55
  • 63