How to run PHP in Kubernetes via unix socket?

11/23/2016

I have nginx and php-fpm containers in Google Container Engine and I would like to run it through unix socket instead of network.

I also have the Google SQL Proxy container and it uses empty volume to provide connection to Google SQL databases through sockets placed on that volume that is shared with other containers. So I was thinking of using the same approach for PHP but it is not working.

I have empty volume of emptyDir type mounted to php and nginx containers. The volume is mounted, I can see it in both containers. But if I try to use nginx I will get (socket)file not found in logs.

I have also added the www-data(same uid as in php container) user into nginx container and set it in nginx conf but it didn't help.

Is Google SQL Proxy special in some way and I have to use network for PHP or am I missing something here?

--
cloud
docker
google-kubernetes-engine
kubernetes

1 Answer

10/19/2017

I managed to get the approach you described working. I use a emptyDir in a deployment so a shared /sock folder is mounted to both nginx and fpm:

  - name: fpm
    ...
    volumeMounts:
    - name: php-socket
      mountPath: /sock
    ...

  - name: nginx
    ...
    volumeMounts:
    - name: php-socket
      mountPath: /sock
    ...

  volumes:
  - name: php-socket
    emptyDir: {}

I found this article helpful for the actual nginx and fpm configuration: https://medium.com/@shrikeh/setting-up-nginx-and-php-fpm-in-docker-with-unix-sockets-6fdfbdc19f91

The snag I hit was a file in my php-fpm docker container config called zz-docker.conf which contains listen = [::]:9000. Since the name of the file is alphabetically high, it was clobbering my other config file, stopping the unix socket communication working (i.e. listen = /sock/php.sock). I removed the problem line using this in the Dockerfile:

sed -i -e ā€˜/listen/dā€™ /usr/local/etc/php-fpm.d/zz-docker.conf
-- Tom
Source: StackOverflow