I am working in a local environment with docker.
I have an nginx web container and a php container which are in the same network.
I build the php container from my own dockerfile (with phpfpm and phpcli); and, the nginx I compose it in a docker-compose from the nginx:stable
hub image.
I have 2 projects: a symfony(http://i-r4y.kaiza.lh/
) and a drupal(http://i-z4r4.kaiza.lh/
) which runs in it. and the symfony exposes an api which have to be consumed by the drupal. The problem is that an error when I call the symfony from the drupal cURL error 7: Failed to connect to i-r4y.kaiza.lh port 80: Connection refused
I thought it was a configuration of the symfony side api route; like it must be public or accept CORS etc …
but when I enter on bash in the php container, and I do curl either the symfony or drupal url, I have the same error.
app@kz-php74:/var/www$ curl http://i-r4y.kaiza.lh
curl: (7) Failed to connect to i-r4y.kaiza.lh port 80: Connection refused
app@kz-php74:/var/www$ curl http://i-z4r4.kaiza.lh
curl: (7) Failed to connect to i-z4r4.kaiza.lh port 80: Connection refused
I checked in the php container that the hosts are present in /etc/hosts
app@kz-php74:/var/www$ cat /etc/hosts | grep i-
127.0.0.1 i-r4y.kaiza.lh
127.0.0.1 i-z4r4.kaiza.lh
Here is the docker-compose.yml :
version: '2.4'
services:
php7.4:
build:
context: ../../../dockerfile
dockerfile: Dockerfile.php
args:
PHP_VERSION: 7.4
container_name: "kz-php74"
hostname: "kz-php74"
user: 1000:1000
working_dir: /var/www
volumes:
- "${LOCAL_PATH}/../www:/var/www"
extra_hosts:
- "i-r4y.kaiza.lh:127.0.0.1"
- "i-z4r4.kaiza.lh:127.0.0.1"
networks:
- kz_local
mysql:
container_name: kz-mysql
image: mariadb:10.4.0
volumes:
- ${LOCAL_PATH}/.data/mariadb:/var/lib/mysql
- ${LOCAL_PATH}/config/mariadb/conf.d/custom.cnf:/etc/mysql/conf.d/custom.cnf
- ${LOCAL_PATH}/../www:/var/www
ports:
- ${MYSQL_PORT:-3306}:3306
environment:
MYSQL_ROOT_PASSWORD: password
networks:
- kz_local
web:
image: nginx:stable
container_name: kz-web
volumes:
- ${LOCAL_PATH}/config/nginx/conf.d:/etc/nginx/conf.d
- ${LOCAL_PATH}/../www:/var/www
ports:
- 80:80
networks:
- kz_local
networks:
kz_local:
external: true
The nginx config of drupal:
server {
listen 80;
listen [::]:80;
server_name i-z4r4.kaiza.lh;
root /var/www/i-z4r4/web;
resolver 127.0.0.11 ipv6=off;
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
# In Drupal 8, we must also match new paths where the '.php' appears in
# the middle, such as update.php/selection. The rule we use is strict,
# and only allows this pattern with the update.php front controller.
# This allows legacy path aliases in the form of
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
# you do not have any paths like that, then you might prefer to use a
# laxer rule, such as:
# location ~ .php(/|$) {
# The laxer rule will continue to work if Drupal uses this new URL
# pattern with front controllers other than update.php in a future
# release.
location ~ '.php$|^/update.php' {
set $fastcgi_pass "kz-php74:9000";
fastcgi_split_path_info ^(.+?.php)(|/.*)$;
# Security note: If you're running a version of PHP older than the
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
# See http://serverfault.com/q/627903/94922 for details.
include fastcgi_params;
# Block httpoxy attacks. See https://httpoxy.org/.
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass $fastcgi_pass;
}
...
upstream php {
server kz-php74:8080;
}
}
For symfony:
server {
listen 80;
listen [::]:80;
server_name i-r4y.kaiza.lh;
root /var/www/i-r4y/public;
resolver 127.0.0.11 ipv6=off;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index.php(/|$) {
set $fastcgi_pass "kz-php74:9000";
fastcgi_pass $fastcgi_pass;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
...
upstream php {
server kz-php74:8080;
}
...
}
will anyone have any idea why this is not working?
thanks
3
Answers
I solved the problem by adding an alias in the network of the web container through which I can access from the php container.
and of course, I needed to add the url aliases in config nginx :
You need to expose port 80 to your docker host.
It looks like you are trying to curl from your docker host (your real machine running docker) to nginx running in a docker container.
You can do that in docker-compose with the following:
This will get you to nginx. However, your next obstacle will probably be ngxinx reaching your php service.
Like @AmyDev mentioned, you’ll be better served using docker’s name resolution for that.
In your nginx config, you need the following line to point nginx at docker’s internal DNS:
Then you can declare your
upstream
with the following:When you run
curl http://i-r4y.kaiza.lh
you make a request to the same container (php7.4), not onweb
(nginx). If you want to make request to another container, you can use the container service name as domain.Try running
curl http://web
in the php container