dynamically adding nginx container ip into phpfpm /etc/hosts file

孤人 提交于 2019-12-12 04:58:13

问题


Looking to automatically add the nginx container ip address inside my phpfpm container /etc/hosts file.

Inside my yml file, I have a service called phpfpm, and I know you can use extra_hosts attribute to assign values into the /etc/hosts file, however I don't know how to dynamically call place the nginx container 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

Any thoughts on how to do this?


回答1:


Containers within a compose file will run on same network and you can just their names. phpfpm and nginx in your case. Also if you need more names for the same service you need to use aliases

  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



回答2:


Why do you need Nginx Ip address? You can call nginx from phpfpm container by hostname nginx



来源:https://stackoverflow.com/questions/45750752/dynamically-adding-nginx-container-ip-into-phpfpm-etc-hosts-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!