title: Multiple docker-compose sharing network with a not yet known host for nginx
body: <p>I use multiple docker-compose files :</p>
  
  <ul>
  <li><p>one for running on the same network : postgres and nginx
  => this containers collection is supposed to be always running</p></li>
  <li><p>one for each asp core web site (each one on a specific port)
  => this containers are updated through a CI/CD pipeline (VSTS)</p></li>
  </ul>
  
  <p>Because Nginx needs to know the hostname when defining the upstream, if the asp core container is not running then it's hostname is not known, then nginx throws an error on <strong>docker-compose up</strong> command :</p>
  
  <pre><code>nginx            | 2018/01/04 15:59:17 [emerg] 1#1: host not found in upstream 
  "webportalstage:5001" in /etc/nginx/nginx.conf:9
   nginx            | nginx: [emerg] host not found in upstream 
   "webportalstage:5001" in /etc/nginx/nginx.conf:9
   nginx exited with code 1
  </code></pre>
  
  <p>And obviously if the asp core container is running before, then nginx knows the hostname <strong>webportalstage</strong> and everything works fine. But the starting sequence is not what I expect.</p>
  
  <p>is there any solution to start nginx with a not yet known hostname  in the upstream ?</p>
  
  <p>Here is my nginx.conf file :</p>
  
  <pre><code>worker_processes 4;
  
   events { worker_connections 1024; }
  
   http {
  sendfile on;
  
  upstream webportalstage {
     server webportalstage:5001;
  }
  
  server {
      listen 80;
  
      location / {
          proxy_pass         http://webportalstage;
          proxy_redirect     off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
      }
  }  
  }
  </code></pre>
  
  <p>And both docker-compose files :
  <strong>Nginx + Postgres :</strong></p>
  
  <pre><code>version: "3"
  
  services:
    proxy:
      image: myPrivateRepo:latest
      ports:
        - "80:80"
      container_name: nginx
      networks:
        aspcore:
          aliases:
            - nginx
  
    postgres:
      image: postgres:latest
      environment:
        - POSTGRES_PASSWORD=myPWD
        - POSTGRES_USER=postgres
      ports:
        - "5432:5432"
      container_name: postgres
      networks:
        aspcore:
          aliases:
            - postgres
  
  networks:
    aspcore:
  driver: bridge
  </code></pre>
  
  <p><strong>One of my asp core web site :</strong></p>
  
  <pre><code>version: "3"
  
  services:
  
    webportal:
      image: myPrivateRepo:latest
      environment:
        - ASPNETCORE_ENVIRONMENT=Staging
      container_name: webportal
      networks:
        common_aspcore:
          aliases:
            - webportal
  
  networks:
    common_aspcore:
      external: true
  </code></pre>