2017-08-18 69 views
0

想要在我的phpfpm容器/ etc/hosts文件中自動添加nginx容器ip地址。動態添加nginx容器ip到phpfpm/etc/hosts文件

在我的yml文件中,我有一個名爲phpfpm的服務,並且我知道你可以使用extra_hosts屬性將值分配到/ etc/hosts文件中,但是我不知道如何動態調用放置nginx容器的IP 。

nginx: 
    build: ./nginx 
    ports: 
     - "80:80" 
     - "443:443" 
    volumes: 
     - ../public/:/var/www/html/public/ 
    container_name: nginx 
    networks: 
     - backend 

    phpfpm: 
    build: ./php-fpm 
    volumes: 
     - ../public/:/var/www/html/public/ 
    container_name: phpfpm 
    extra_hosts: 
     - "test.local:nginx" <insert nginx ip to test.local> 
    networks: 
     - backend 

有關如何做到這一點的任何想法?

回答

0

撰寫文件中的容器將在同一網絡上運行,您可以只使用它們的名稱。 phpfpmnginx你的情況。此外,如果你需要更多的名字相同的服務,你需要的別名

nginx: 
    build: ./nginx 
    ports: 
     - "80:80" 
     - "443:443" 
    volumes: 
     - ../public/:/var/www/html/public/ 
    container_name: nginx 
    networks: 
     backend: 
     aliases: 
      - test.local 

    phpfpm: 
    build: ./php-fpm 
    volumes: 
     - ../public/:/var/www/html/public/ 
    container_name: phpfpm 
    networks: 
     - backend 
+0

是的,因爲有多個網站坐在指向phpfpm容器的nginx上,其中一個腳本想要對特定網站進行卷曲。所以我需要將域綁定到一個IP,以便它可以調用正確的文件。 – yohox3

+0

因此,在你的代碼中,如果捲髮'test.local'沒有定義'extra_hosts'並且定義了別名,它就會工作 –

+0

感謝這很好,我沒有看到他們有這個功能! – yohox3

0

爲什麼你需要Nginx的IP地址?您可以通過主機名稱從phpfpm容器中調用nginx nginx

+0

我有我的PHP容器內的腳本,讓我來管理多個WordPress的網站使用,而且好像是腳本使用捲曲,以獲取網站並登錄。由於phpfpm和nginx位於不同的容器上,因此手動將域綁定到主機文件可以解決問題。 – yohox3