This is kinda a newbie question since I'm still trying to understand how containers "communicate" to each other.
This is roughly what my docker-compose.yml looks like
...
api:
build: ./api
container_name: api
volumes:
- $HOME/devs/apps/api:/var/www/api
laravel:
build: ./laravel
container_name: laravel
volumes:
- $HOME/devs/apps/laravel:/var/www/laravel
depends_on:
- api
links:
- api
...
nginx-proxy:
build: ./nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
links:
- api
- laravel
- mysql-api
nginx configs have blocks referring to upstream exposed by those 2 php-fpm containers, like this:
location ~* \.php$ {
fastcgi_pass laravel:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
similar for the api block.
I can hit each container individually from the web browser/postman (from the host).
Inside the laravel app, there is some php_curl to call a REST service exposed by the api service. I got 500, with this error (from the nginx container):
PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /var/www/laravel/vendor/symfony/debug/Exception/FatalErrorException.php on line 1" while reading response header from upstream, client: 172.22.0.1, server: laravel.lo, request: "POST {route_name} HTTP/1.1", upstream: "fastcgi://172.22.0.5:9000", host: "laravel.lo"
I tried hitting the api from the laravel container using wget
root@a34903360679:/app# wget api.lo
--2018-08-01 09:57:51-- http://api.lo/
Resolving api.lo (api.lo)... 127.0.0.1
Connecting to api.lo (api.lo)|127.0.0.1|:80... failed: Connection refused.
It resolves to localhost, but I believe 127.0.0.1 in this context seems to be the laravel container itself, not the host/nginx services. I used to have all the services in a single centos VM for development, which didn't have this problem.
Can anyone give some advice on how I could achieve this environment?
EDIT: I found the answer (not long after posting this question). Refer to here: https://medium.com/@yani/two-way-link-with-docker-compose-8e774887be41
To get the laravel container reaches back to nginx services (so nginx can resolve api request to the api container), use internal network. So something like:
networks:
internal-api:
Then alias the laravel and nginx containers, like so:
laravel:
...
networks:
internal-api:
aliases:
- laravel
...
nginx-proxy:
...
networks:
internal-api:
aliases:
- api
networks:
internal-api: