Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: multiple PHP7-FPM containers on different ports

Tags:

php

docker

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"
like image 975
Esben Avatar asked Jan 26 '16 14:01

Esben


People also ask

Can multiple containers run on same port?

So there is no conflict if multiple containers are using the same port ( :80 in this case). You can access one container from another using its container-name or service-name or ip-address, whereas ip-address is not a good idea because this might change every time you (re)start the container.

Can two Docker containers expose same port?

Surprisingly or not, neither Docker nor Podman support exposing multiple containers on the same host's port right out of the box. Example: docker-compose failing scenario with "Service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash."

Can one Docker image have multiple containers?

Multiple containers can run simultaneously, each based on the same or different images. Docker is similar to virtual machines in the way it creates multiple instances of an operating system. However, Docker lets you create containers that run on the same operating system.


1 Answers

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.

like image 145
Dracken Avatar answered Oct 21 '22 16:10

Dracken