Esben
Esben

Reputation: 1971

Docker: multiple PHP7-FPM containers on different ports

I am playing around with Docker for my local development environment. My setup for now is 5 containers (1 HaProxy + 2 NGINX + 2 PHP7-FPM).

The proxy container is used to direct the request based on the url, so if I enter http://project1.dev it will proxy the request to the project1-nginx that uses project1-php for evaluating php. The setup is similar for http://project2.dev.

Now, I am trying to wrap my head around the ports of the two php containers. The default fpm port is 9000, so both php containers cannot run on this. I am assuming the way to go here is to let both containers export port 9000 but make them 9000 and 9001 on the host?

Something along these lines in my compose file.

project_1_php:
  ports:
    - "9000:9000"
project_2_php:
  ports:
    - "9001:9000"

So, everything boots up fine, and project 1 is working, but project 2 gives me a 502. Nginx error log says

2016/01/26 14:37:05 [error] 6#6: *1 connect() failed (111: Connection refused) 
while connecting to upstream, client: 172.17.0.9, server: code.dev, 
request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.4:9001"

Upvotes: 10

Views: 9197

Answers (3)

Shōgun8
Shōgun8

Reputation: 572

Just thought I would mention that Traefik Proxy is a fantastic way to handle this scenario

Upvotes: -1

Dracken
Dracken

Reputation: 126

For those looking like I did for a way to run multiple NGINX and PHP-FPM containers for different projects at the same time and found this SO thread, ran across this:

https://github.com/docker-library/php/issues/479

Inside the php-fpm Dockerfile:

FROM php:7.2-fpm
RUN sed -i 's/9000/3001/' /usr/local/etc/php-fpm.d/zz-docker.conf

Then in your docker-compose.yaml file you can point your Nginx to that specific port for that PHP-FPM instance.

Upvotes: 10

2oppin
2oppin

Reputation: 2001

Had similar issue with php-fpm7, as @Mjh mentioned in the comments, by default fpm listening to 127.0.0.1:9000,

so you should replace it with a 0.0.0.0:9000,

I found a solution there: githib:matriphe/docker-alpine-nginx

So you can add to your fpm container Dockerfile :

RUN sed -i "s|;*listen\s*=\s*127.0.0.1:9000|listen = 9000|g" /etc/php7/php-fpm.conf

Upvotes: 0

Related Questions