dynamically adding nginx container ip into phpfpm

2019-08-18 04:23发布

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?

2条回答
ら.Afraid
2楼-- · 2019-08-18 04:30

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

查看更多
Luminary・发光体
3楼-- · 2019-08-18 04:35

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
查看更多
登录 后发表回答