Docker - can't connect to php7.0-fpm.sock
Pergunta
I'm trying to set up a Magento2 dev environment on Docker with PHP7 & nginx but I'm getting the following error:
$ docker logs --details mymagento2docker_nginx_1
2016/10/04 19:18:31 [crit] 7#7: *2 connect() to unix:/var/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 172.17.0.1, server: magento2.docker, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "magento2.docker"
172.17.0.1 - - [04/Oct/2016:19:18:31 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36" "-"
I imagine it is because I don't have upstream fastcgi_backend
configured correctly in my nginx container
or that it isn't installed in my php container
.
I also can't find the php7.0-fpm.sock
within the php container
. I've tried looking for it with:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------
mymagento2docker_app_1 true Exit 0
mymagento2docker_data_1 docker-entrypoint.sh true Exit 0
mymagento2docker_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
mymagento2docker_nginx_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:80->80/tcp
mymagento2docker_php_1 php-fpm Up 9000/tcp
mymagento2docker_phpmyadmin_1 /run.sh phpmyadmin Up 0.0.0.0:8080->80/tcp
$ docker exec -it mymagento2docker_php_1 /bin/bash
root@e220b07a7124:/var/www/html# php -v
PHP 7.0.11 (cli) (built: Sep 23 2016 21:47:48) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
root@e220b07a7124:/var/www/html# ls /var/run/php
ls: cannot access /var/run/php: No such file or directory
root@e220b07a7124:/var/www/html# ls /var/run/
exim4 lock utmp
root@e220b07a7124:/var/www/html# find / -name "*sock"
/sys/devices/virtual/misc/vsock
/sys/class/misc/vsock
/sys/module/vsock
/sys/module/hv_sock
root@e220b07a7124:/var/www/html# find / -type d -path /var/www/html -prune -o -name "php"
/usr/local/lib/php
/usr/local/etc/php
/usr/local/include/php
/usr/local/bin/php
/usr/local/php
/usr/local/php/php
/var/www/html
This is my Docker Project set up:
├── docker-compose.yml
├── magento2
│ ├── [Lots ommited...]
│ ├── [Lots ommited...]
├── nginx
│ ├── Dockerfile
│ ├── default.conf
│ └── nginx.conf
└── php
└── Dockerfile
docker-composer.yml
# http://tech.osteel.me/posts/2015/12/18/from-vagrant-to-docker-how-to-use-docker-for-local-web-development.html#installation
# composer create-project --ignore-platform-reqs --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
build: ./php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
app:
image: php:7.0-fpm
volumes:
- ./magento2:/var/www/html
command: "true"
mysql:
image: mysql:latest
volumes_from:
- data
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mage2
MYSQL_DATABASE: mage2
MYSQL_USER: mage2
MYSQL_PASSWORD: mage2
data:
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
links:
- mysql
environment:
PMA_HOST: mysql
php Dockerfile
FROM php:7.0-fpm
# Install dependencies
RUN apt-get update \
&& apt-get install -y \
cron \
libfreetype6-dev \
libicu-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libxslt1-dev
# Configure the gd library
RUN docker-php-ext-configure \
gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
# Install required PHP extensions
RUN docker-php-ext-install \
gd \
intl \
mbstring \
mcrypt \
pdo_mysql \
xsl \
zip \
soap
# Install the 2.4 version of xdebug that's compatible with php7
RUN pecl install -o -f xdebug-2.4.0
nginx Dockerfile
FROM nginx:latest
RUN rm -f /etc/nginx/conf.d/*
RUN rm -f /etc/nginx/nginx.conf
COPY ./nginx.conf /etc/nginx/
COPY ./default.conf /etc/nginx/conf.d/default.conf
nginx default.conf
# Example configuration:
upstream fastcgi_backend {
# use tcp connection
server unix:/var/run/php/php7.0-fpm.sock;
# I"ve also tried the below...
# server 127.0.0.1:9000;
# server fpm:9000;
}
server {
listen 80;
server_name magento2.docker;
set $MAGE_ROOT /var/www/html/;
set $MAGE_MODE default;
root $MAGE_ROOT/pub;
index index.php;
autoindex off;
charset off;
add_header 'X-Content-Type-Options' 'nosniff';
add_header 'X-XSS-Protection' '1; mode=block';
location /setup {
root $MAGE_ROOT;
location ~ ^/setup/index.php {
fastcgi_pass fastcgi_backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/setup/(?!pub/). {
deny all;
}
location ~ ^/setup/pub/ {
add_header X-Frame-Options "SAMEORIGIN";
}
}
location /update {
root $MAGE_ROOT;
location ~ ^/update/index.php {
fastcgi_split_path_info ^(/update/index.php)(/.+)$;
fastcgi_pass fastcgi_backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# deny everything but index.php
location ~ ^/update/(?!pub/). {
deny all;
}
location ~ ^/update/pub/ {
add_header X-Frame-Options "SAMEORIGIN";
}
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location /pub {
location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
deny all;
}
alias $MAGE_ROOT/pub;
add_header X-Frame-Options "SAMEORIGIN";
}
location /static/ {
if ($MAGE_MODE = "production") {
expires max;
}
location ~ ^/static/version {
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/ {
try_files $uri $uri/ /get.php?$args;
location ~ ^/media/theme_customization/.*\.xml {
deny all;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
try_files $uri $uri/ /get.php?$args;
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
try_files $uri $uri/ /get.php?$args;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/customer/ {
deny all;
}
location /media/downloadable/ {
deny all;
}
location /media/import/ {
deny all;
}
location ~ cron\.php {
deny all;
}
location ~ (index|get|static|report|404|503)\.php$ {
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=512M \n max_execution_time=600";
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_param MAGE_MODE $MAGE_MODE;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx nginx.conf
user root; worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
gzip on;
include /etc/nginx/conf.d/*;
}
Solução
In nginx configuration try use
upstream fastcgi_backend {
server php:9000;
}
Outras dicas
I also can't find the php7.0-fpm.sock within the php container. I've tried looking for it with:
I think You miss php7.0-fpm extension in this docker, Check you installed or not php7.0-fpm in docker
if you installed php7.0-fpm in docker go to php info Which php.ini file used in php.
Nginx + magento2 Need /etc/php/7.0/fpm/php.ini file not /etc/php/7.0/cli/php.ini
If you Php used /etc/php/7.0/fpm/php.ini then go to /etc/php/7.0/fpm/pool.d/www.conf
change
listern = 127.0.0.1 :9000
to
listen = /run/php/php7.0-fpm.sock
then only create php7.0-fpm.sock file.
After Restart the php7.0-fpm.
service php7.0-fpm restart
Now to go nginx default.conf change below line.
upstream fastcgi_backend {
# use tcp connection
server unix:/run/php/php7.0-fpm.sock;
}
Now Check it. I hope this is useful for you, Same Problem Occur Before, I try to change this, its worked for me. If you have any issue after change this configuration, let me know
I'm using laradock and faced the same issue. Solution for me was the following:
Change all lines fastcgi_pass fastcgi_backend;
to fastcgi_pass php-upstream;
and don't use upstream fastcgi_backend {...}
section