7
votes

There is my docker-compose.yml

version: '2'

services:
    web:
        image: nginx:latest
        ports:
            - "8018:80"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf
            - /private/var/log/nginx:/var/log/nginx
            - /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./code:/code
            - ./php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ./www.conf:/usr/local/etc/php-fpm.d/www.conf
            - /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

And in the site.conf I write like this fastcgi_pass unix:/var/run/php7-fpm.sock;I also change the listen address to listen = /var/run/php7-fpm.sock in www.conf. And in my MAC, there is a file named php7-fpm.sock in folder /private/var/run with mode 666

After I ran docker-compose up -d, the containers was running success.But when I visited http://localhost:8018, it returned 502. After I checked the nginx error log, I found out this

2017/11/01 13:08:39 [error] 6#6: *1 connect() to unix:/var/run/php7-fpm.sock failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "localhost:8018"

Btw, before I tried using unix socket mode. I succeed in visiting http://localhost:8018 with tcp/ip mode.

1
Can you post your nginx configuration?Stefano
This really helped me learn php/nginx for docker: serversforhackers.com/s/docker-in-developmentJeff Puckett

1 Answers

9
votes

After I check the How to set up Apache2 and PHP-FPM via unix socket?, I changed my docker-compose.yml to

version: '2'

services:
    web:
        image: nginx:latest
        ports:
            - "8018:80"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf
            - /private/var/log/nginx:/var/log/nginx
            - "phpsocket:/var/run"
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./code:/code
            - ./php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ./www.conf:/usr/local/etc/php-fpm.d/www.conf
            - ./zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
            - "phpsocket:/var/run"
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

volumes:
    phpsocket:

And override zz-docker.conf to

[global]
daemonize = no

[www]
listen = /var/run/php7-fpm.sock
listen.mode = 0666

Finally When I visited http://localhost:8018, phpinfo page showed up!