I am new to docker. I would like to create a webapplication that contains of microservices.
To start and experiment, I have created an nginx container, 2 php containers and a mysql container.
The nginx container is accessible from the host on port 8080.
Both php containers are exposed on port 9000 which is only accessible from within the docker network.
All 4 containers are connected to the same docker network.
Now I try to make a call from one php container to the other with the following PHP command:
Use docker-compose to create your services. Then you can use the names of the services as hostnames to connect from one container to another. Notice that you connect to the service itself, not to its published port on the host (for example connect to port 80 for the nginx service).
I have used docker-compose to create the services. They are all in the same docker-compose file. For the mysql container I am able to connect to it with the name of the service.
Also nginx is able to connect to them in the Server Blocks. I have created 2 Server Blocks that are able to connect to the 2 php containers through the name of the service (one on port 8080 and the other one on port 8081).
The only thing that does not work is the communication between the 2 php containers directly.
When I am home this evening, I will post the content of my files. Maybe I did something wrong in there.
Howdy.
What network type are you using? Bulit-in Bridge? or User-Defiend bridge?
To access containers by way of container name, you must attach all your containers to a custom network. Otherwise, the boxes can only be reached by IP address.
Docker networks can be connected to running containers, as well.
I cite from PHP-FPM on cwiki.apache.org:
“The default pool, [www] , has this configured as listen 127.0.0.1:9000 : it will only respond to requests on the local loopback network interface (localhost), on TCP port 9000”
So you should probably check the config for php-fpm and change the listen address to 0.0.0.0:9000.
I see some more issues with your compose file:
If you are in development, use highest available version 2 (2.4 at the moment).
Don’t add restart commands. Restarting containers may make it very difficult to find errors.
Don’t create a network if you don’t have a very specific reason to do so.
Only publish the ports that you need on the frontend.
If I go to localhost:8080 I get the results of php1:9000, so the container is accessible from the nginx container.
although, If I try to access the container from within container php2, again I get:
I have tried another configuration where I use 2 php7.3-apache containers and this configuration does not give any errors. These off-course communicate over apache and not directly through the php setup.
Is there a way I can have 1 nginx container with multiple php containers where the php containers are able to communicate with each other?