How to correctly link php-fpm and Nginx Docker containers?

后端 未结 7 994
天涯浪人
天涯浪人 2020-12-02 04:20

I am trying to link 2 separate containers:

  • nginx:latest
  • php:fpm

The problem is that php scripts do not work. Perhaps the php-fpm config

7条回答
  •  天涯浪人
    2020-12-02 04:29

    Don't hardcode ip of containers in nginx config, docker link adds the hostname of the linked machine to the hosts file of the container and you should be able to ping by hostname.

    EDIT: Docker 1.9 Networking no longer requires you to link containers, when multiple containers are connected to the same network, their hosts file will be updated so they can reach each other by hostname.

    Every time a docker container spins up from an image (even stop/start-ing an existing container) the containers get new ip's assigned by the docker host. These ip's are not in the same subnet as your actual machines.

    see docker linking docs (this is what compose uses in the background)

    but more clearly explained in the docker-compose docs on links & expose

    links

    links:
     - db
     - db:database
     - redis
    

    An entry with the alias' name will be created in /etc/hosts inside containers for this service, e.g:

    172.17.2.186  db
    172.17.2.186  database
    172.17.2.187  redis
    

    expose

    Expose ports without publishing them to the host machine - they'll only be accessible to linked services. Only the internal port can be specified.

    and if you set up your project to get the ports + other credentials through environment variables, links automatically set a bunch of system variables:

    To see what environment variables are available to a service, run docker-compose run SERVICE env.

    name_PORT

    Full URL, e.g. DB_PORT=tcp://172.17.0.5:5432

    name_PORT_num_protocol

    Full URL, e.g. DB_PORT_5432_TCP=tcp://172.17.0.5:5432

    name_PORT_num_protocol_ADDR

    Container's IP address, e.g. DB_PORT_5432_TCP_ADDR=172.17.0.5

    name_PORT_num_protocol_PORT

    Exposed port number, e.g. DB_PORT_5432_TCP_PORT=5432

    name_PORT_num_protocol_PROTO

    Protocol (tcp or udp), e.g. DB_PORT_5432_TCP_PROTO=tcp

    name_NAME

    Fully qualified container name, e.g. DB_1_NAME=/myapp_web_1/myapp_db_1

提交回复
热议问题