A
A
alex5e2018-11-12 17:02:54
PHP
alex5e, 2018-11-12 17:02:54

How to properly configure docker containers for curl to work correctly in php?

There is docker-compose , in which nginx and php are in separate containers

version: '3'

services:

    php:
        build:
            context: ./docker/php
        ports:
            - ${PORT_PHP}:9000
        volumes:
            - ${PATH_PROJECT}:/srv/www/promo
            - ${PATH_STATIC}:/srv/www/promo-static
            - ./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
            - ./docker/php/custom.ini:/usr/local/etc/php/conf.d/custom.ini
        environment:
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}

    nginx:
        image: nginx:1.13.8
        ports:
            - ${PORT_NGINX}:80
        volumes:
            - ${PATH_PROJECT}:/srv/www/promo
            - ${PATH_STATIC}:/srv/www/promo-static
            - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php
        environment:
            NGINX_HOST: ${NGINX_HOST}
            PORT_PHP: ${PORT_PHP}

    mysql:
        image: mysql:5.7
        ports:
            - ${PORT_MYSQL}:3306
        depends_on:
            - php
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}

In /system32/drivers/etc/hosts
127.0.0.1 promo.mdtest.org
In .env
...
PORT_PHP=9002
PORT_NGINX=8002
PORT_MYSQL=3308

promo.mdtest.org:8002/promo-static/build/css/compo... opens in browser, but doesn't open with curl in php script.
The request via curl only works if $_SERVER['SERVER_ADDR'] (172.19.0.4) is specified in the php script instead of $_SERVER['HTTP_HOST'] (promo.mdtest.org:8002)
How to set up docker-compose so that curl works like a browser ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2019-01-08
@ivankomolin

It's not about docker-compose.
When you open the promo.mdtest.org site page through a browser, you use address resolution on the host.
When you open the promo.mdtest.org site page through the php container, you use address resolution in the php container.
If you want to get the same results, you need to have access to this site inside the container too.
Because docker-compose creates a network for all the specified containers on startup, then you can access this site inside the container, like this:
curl nginx -H 'Host: promo.mdtest.org'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question