Docker - Communication between two PHP containers: "Connection Refused" - php

So, I have a problem with my docker setup for a project I'm working on. Using Docker and docker-compose I have set up a few containers as seen below. I can reach both php services via the browser no problem. dev.drmobile.local refers to the Magento service and dev.dronline.local refers to the Symfony service. The problem is that when the Magento service needs data from the Symfony service, I get an error stating Unable to Connect to tcp://dev.dronline.local:80. Error #111: Connection refused. I have tried merging both services in 1 container, but the problem is also present in that situation.
All other communication between containers seems to work fine.
I'm not sure on the port setup I've used, but it doesn't seem to trigger any errors.
Both php containers use the same image created from a Dockerfile, with a slightly different init script.
Any help and tips will be greatly appreciated.
docker-compose.yml
version: '3.3'
networks:
drmobile:
services:
nginx-service:
image: nginx:stable-alpine
container_name: drmobile-nginx-container
ports:
- "8080:80"
volumes:
- ./:/var/www/html
- ./.docker/nginx/:/etc/nginx/conf.d/
depends_on:
- symfony-service
- magento-service
networks:
- drmobile
magento-service:
container_name: drmobile-magento-container
build:
context: .
dockerfile: ./.docker/php/Dockerfile
ports:
- "9000:9000"
volumes:
- ./:/var/www/html
- ./.docker/php/php.ini:/usr/local/etc/php/php.ini
- ./.docker/php/magento/docker-install.sh:/var/www/html/docker-install.sh
networks:
- drmobile
symfony-service:
container_name: drmobile-symfony-container
build:
context: .
dockerfile: ./.docker/php/Dockerfile
ports:
- "9001:9000"
volumes:
- ./:/var/www/html
- ./.docker/php/php.ini:/usr/local/etc/php/php.ini
- ./.docker/php/symfony/docker-install.sh:/var/www/html/docker-install.sh
networks:
- drmobile
mysql-service:
image: mysql:5.7
container_name: drmobile-mysql-container
ports:
- "3306:3306"
volumes:
- ./.docker/data/mysql-data:/var/lib/mysql
- ./.docker/mysql/:/docker-entrypoint-initdb.d/
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: dynalinq
MYSQL_PASSWORD: password
networks:
- drmobile
redis-service:
image: redis:2.8
container_name: drmobile-redis-container
ports:
- "6379:6379"
networks:
- drmobile
varnish-service:
build:
context: .docker/varnish
container_name: drmobile-varnish-container
ports:
- "80:80"
networks:
- drmobile
depends_on:
- nginx-service
elasticsearch:
image: elasticsearch:1.7.6
container_name: drmobile-elasticsearch-container
networks:
- drmobile
volumes:
- ./.docker/data/elasticsearch-data:/usr/share/elasticsearch/data
environment:
ES_JAVA_OPTS: "-Xmx512m -Xms512m"
HOSTNAME: "elasticsearch-dev"
.docker/nginx/dev.dronline.local.conf
server {
listen 80;
root /var/www/html/symfony/web;
server_name dev.dronline.local;
access_log /var/log/nginx/dronline_access.log custom_dronline;
error_log /var/log/nginx/dronline_error.log;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass drmobile-symfony-container:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
include fastcgi_params;
}
}
.docker/nginx/dev.drmobile.local.conf
server {
listen 80 default_server;
server_name dev.drmobile.local;
root /var/www/html/magento;
index index.html index.php;
access_log /var/log/nginx/dev.drmobile.local_access.log custom;
error_log /var/log/nginx/dev.drmobile.local_error.log;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
expires 30d; ## Assume all files are cachable
}
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php$ { ## Execute PHP scripts
fastcgi_pass drmobile-magento-container:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE base;
fastcgi_param MAGE_RUN_TYPE website;
include fastcgi_params;
}
}

Related

Mezzio app works fine on built-in php server but gives error when run in docker container

I've got a Mezzio application that works perfectly on built-in php server using compoer serve, however, when I try it in docker environment, it gives me following error.
Fatal error: Uncaught Laminas\View\Exception\RuntimeException: Laminas\View\Renderer\PhpRenderer::render: Unable to render template "error::error"; resolver could not resolve to a file in /var/www/portal/vendor/laminas/laminas-view/src/Renderer/PhpRenderer.php:492 Stack trace: #0 /var/www/portal/vendor/mezzio/mezzio-laminasviewrenderer/src/LaminasViewRenderer.php(232): Laminas\View\Renderer\PhpRenderer->render(NULL) #1 /var/www/portal/vendor/mezzio/mezzio-laminasviewrenderer/src/LaminasViewRenderer.php(221): Mezzio\LaminasView\LaminasViewRenderer->renderModel(Object(Laminas\View\Model\ViewModel), Object(Laminas\View\Renderer\PhpRenderer), Object(Laminas\View\Model\ViewModel)) #2 /var/www/portal/vendor/mezzio/mezzio-laminasviewrenderer/src/LaminasViewRenderer.php(138): Mezzio\LaminasView\LaminasViewRenderer->renderModel(Object(Laminas\View\Model\ViewModel), Object(Laminas\View\Renderer\PhpRenderer)) #3 /var/www/portal/vendor/mezzio/mezzio/src/Response/ErrorResponseGeneratorTrait.php(61): Mezzio\LaminasView\LaminasViewRende in /var/www/portal/vendor/laminas/laminas-view/src/Renderer/PhpRenderer.php on line 492
NGINX proxy (nginx.conf)
user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
}
http {
upstream nginx-web {
server web:8110;
}
upstream nginx-portal {
server portal:8115;
}
upstream nginx-api {
server api:8120;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
server {
listen 80;
server_name proxy;
location /web {
proxy_pass http://nginx-web;
}
location /portal {
proxy_pass http://nginx-portal;
}
location /api {
proxy_pass http://nginx-api;
}
}
}
Server Block (default.conf) portal
server {
listen 8110;
root /var/www/portal/public;
server_name proxy;
location / {
try_files $uri $uri/ /index.php$is_args$args;
index index.php;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
docker-compose.yml
version: "3.9"
services:
db:
container_name: db
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- "${PROJECT_ROOT}/db:/var/lib/mysql"
- ./conf/db/my.cnf:/etc/mysql/conf.d/my.cnf:ro
env_file:
- .env
networks:
- backend
proxy:
container_name: proxy
image: nginx:mainline-alpine
volumes:
- ./conf/proxy/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "80:80"
- "8110:8110"
# - "8115:8115"
- "8120:8120"
depends_on:
- web
- portal
- api
networks:
- backend
web:
container_name: web
image: nginx:mainline-alpine
volumes:
- "${PROJECT_ROOT}/web:/var/www/web"
- ./conf/web/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- php
networks:
- backend
portal:
container_name: portal
image: nginx:mainline-alpine
volumes:
- "${PROJECT_ROOT}/portal:/var/www/portal"
- ./conf/portal/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- db
- php
port:
- "8115:8115"
networks:
- backend
api:
container_name: api
image: nginx:mainline-alpine
volumes:
- "${PROJECT_ROOT}/api:/var/www/api"
- ./conf/api/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- php
networks:
- backend
php:
container_name: php
image: maciejslawik/php-fpm-xdebug:latest
ports:
- "9000:9000"
volumes:
- "${PROJECT_ROOT}/web:/var/www/web"
- "${PROJECT_ROOT}/portal:/var/www/portal"
- "${PROJECT_ROOT}/api:/var/www/api"
- ./conf/php/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini:ro
networks:
- backend
composer:
container_name: composer
image: composer:2
restart: "no"
command: install
volumes:
- "${PROJECT_ROOT}/composer:/app"
- ./composer/composer.json:/app/composer.json
networks:
- backend
phinx:
container_name: phinx
image: vegbrasil/phinx:latest
volumes:
- "${PROJECT_ROOT}/migrations/database/migrations:/app/database/migrations"
- "${PROJECT_ROOT}/migrations/database/seeds:/app/database/seeds"
- ./conf/phinx/phinx.php:/app/phinx.php:ro
env_file:
- .env
depends_on:
- db
restart: "no"
command: "status"
environment:
- TESTS_PHINX_DB_ADAPTER_MYSQL_HOST=db
networks:
- backend
schemaspy:
container_name: schemaspy
image: schemaspy/schemaspy:latest
volumes:
- "${PROJECT_ROOT}/schemaspy/output:/output"
- ./conf/schemaspy/schemaspy.properties:/schemaspy.properties:ro
restart: "no"
depends_on:
- db
networks:
- backend
networks:
backend:
Update
I've tried to xdebug the issue. What I've figured out is that .phtml template file is not being resolved by PhpRenderer.php. Also I'm accessing it with url http://web.local:8115/dashboard however, ideally, I'd like to access it as http://web.local/portal/dashboard.
Suggestions
Could it be that default.conf file is processing only .php files and not .phtml files, however, I doubt it as the issue is, there's a line $file->isReadable() isn't able to process the file whereas I've verified that the file does exist. i.e. summary.phtml.
The permission for this file is also fine.
drwxrwxrwx 1 root root 4096 Oct 31 07:57 .
drwxrwxrwx 1 root root 4096 Oct 31 07:57 ..
-rwxrwxrwx 1 root root 197 Oct 31 07:57 summary.phtml

Docker-Compose with Nginx and PHP -> Nginx Config for Performance

Since a few weeks i'm working with Docker/Docker-Compose and Linux.
I'm wondering if it's possible to tune my nginx Performance.
All the info I found online where for standard Nginx on Linux and not in a Docker Container.
Here you can see my Docker-Compose and my Nginx default conf.
Maybe you can give me a hint on what I can do better.
Thanks
Docker-Compose----------------------------------------
version: '3.7'
services:
traefik:
image: traefik:v2.4
container_name: traefik
restart: always
security_opt:
- no-new-privileges:true
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./apps/traefik/traefik.yml:/traefik.yml:ro
- ./apps/traefik/acme.json:/acme.json
- ./apps/traefik/configurations:/configurations
networks:
- proxy
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=websecure"
- "traefik.http.routers.traefik.rule=Host(`traefik.domain.com`)"
- "traefik.http.routers.traefik.service=api#internal"
- "traefik.http.routers.traefik.middlewares=user-auth#file"
nginx:
image: nginx:latest
container_name: nginx
restart: always
volumes:
- ./www:/www
- ./apps/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- proxy
- internal
links:
- php-fpm
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.nginx.entrypoints=websecure"
- "traefik.http.routers.nginx.rule=Host(`domain.com`) || Host(`www.domain.com`)"
php-fpm:
image: bitnami/php-fpm:latest
volumes:
- ./www:/www
expose:
- 9000
networks:
- internal
mariadb:
image: mariadb:latest
container_name: mariadb
volumes:
- ./apps/mariaDB/db-data:/var/lib/mysql
networks:
db-net:
internal:
host-network:
ipv4_address: 172.22.0.100
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DATABASE: db
MYSQL_USER: db-user
MYSQL_PASSWORD: 123
networks:
proxy:
driver: host
external: true
db-net:
internal: true
internal:
internal: true
host-network:
ipam:
driver: default
config:
- subnet: 172.22.0.0/16
Nginx-Conf-----------------------------------------
Server {
server_tokens off;
listen 80;
server_name www.domain.com;
error_log /www/log/error.log;
access_log /www/log/access.log;
root /www;
location / {
index index.php index.html;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~* \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
According to the answer provided in this post you can increase the performance of the nginx container by running it in the Docker bridge network.
Additionally there is another answer that says that running the container in the Docker bridge network it gives better performance as it uses the host's native networking.

Issue with docker (nginx-proxy, phpfpm, mysql containers) for multiple websites

The issue is: php files are downloaded instead of executing.
I know there are some other similar questions out there to this one. I read many of them but either their scenerios are different or they have no correct answers. I have spent days and tried many ways but still can't figure it out. Therefore I am asking for help. The following are the scenerio, together with the .yml .conf files.
network: nginx-proxy
nginx-proxy:
- nginx (proxy) container
- docker-gen container
- letsencrypt container
web1: (this works fine)
- mysql container
- wordpress container (from wordpress image)
web2: (this has the issue: php files downloaded instead of executing)
- mysql container
- phpfpm container
- nginx container (env: VIRTUAL_HOST, LETSENCRYPT_HOST)
As mentioned above, if I use a Wordpress image, it works successfully. No hassle at all.
However, when I try to set up one with php, nginx and mysql containers myself, those php files will be downloaded instead of executing. (html files can be executed)
create network:
docker network create nginx-proxy
nginx-proxy: docker-compose.yml
version: '3'
services:
nginx:
image: nginx:1.13.1
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- certs:/etc/nginx/certs
- ./sites-enabled:/etc/nginx/sites-enabled
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
dockergen:
image: jwilder/docker-gen:0.7.3
container_name: nginx-proxy-gen
depends_on:
- nginx
command: -notify-sighup nginx-proxy -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
volumes:
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- certs:/etc/nginx/certs
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro
- ./sites-enabled:/etc/nginx/sites-enabled
letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginx-proxy-le
depends_on:
- nginx
- dockergen
environment:
NGINX_PROXY_CONTAINER: nginx-proxy
NGINX_DOCKER_GEN_CONTAINER: nginx-proxy-gen
volumes:
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- certs:/etc/nginx/certs
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./sites-enabled:/etc/nginx/sites-enabled
volumes:
conf:
vhost:
html:
certs:
networks:
default:
external:
name: nginx-proxy
nginx.conf
# web1.local
upstream web1.local {
## Can be connected with "nginx-proxy" network
# wordpress_web1
server 192.168.96.3:80;
}
server {
server_name web1.local;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
include /etc/nginx/vhost.d/default;
location / {
proxy_pass http://web1.local;
}
}
server {
server_name web1.local;
listen 443 ssl http2 ;
access_log /var/log/nginx/access.log vhost;
return 500;
ssl_certificate /etc/nginx/certs/default.crt;
ssl_certificate_key /etc/nginx/certs/default.key;
}
# web2.local
upstream web2.local {
## Can be connected with "nginx-proxy" network
# nginx_web2
server 192.168.96.7:80;
}
server {
server_name web2.local;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location / {
proxy_pass http://web2.local;
}
}
server {
server_name web2.local;
listen 443 ssl http2 ;
access_log /var/log/nginx/access.log vhost;
return 500;
ssl_certificate /etc/nginx/certs/default.crt;
ssl_certificate_key /etc/nginx/certs/default.key;
}
sites-enabled/web2.conf
(I also tried to put this part in default.conf but it didn't work)
server {
index index.php index.html index.htm;
root /var/www/html;
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_pass phpfpm_web2:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
apps:
web1 (Wordpress): docker-compose.yml (This works fine)
version: "3"
services:
mysql_web1:
container_name: mysql_web1
image: mysql:5.7
restart: always
volumes:
- db_data_web1:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db1
MYSQL_USER: user1
MYSQL_PASSWORD: password
wordpress_web1:
container_name: wordpress_web1
image: wordpress:latest
restart: always
expose:
- 80
depends_on:
- mysql_web1
environment:
VIRTUAL_HOST: web1.local
LETSENCRYPT_HOST: web1.local
LETSENCRYPT_EMAIL: foo#web1.local
WORDPRESS_DB_HOST: mysql_web1:3306
WORDPRESS_DB_USER: user1
WORDPRESS_DB_PASSWORD: password
volumes:
db_data_web1:
networks:
default:
external:
name: nginx-proxy
web2: docker-compose.yml
version: "3"
services:
mysql_web2:
container_name: mysql_web2
image: mysql:5.7
restart: always
ports:
- 3306
expose:
- 3306
volumes:
- db_data_web2:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: db2
MYSQL_USER: user2
MYSQL_PASSWORD: password
phpfpm_web2:
container_name: phpfpm_web2
image: php-fpm:latest
restart: always
ports:
- 9000
expose:
- 9000
links:
- mysql_web2
depends_on:
- mysql_web2
volumes:
- ./code:/var/www/html
nginx_web2:
container_name: nginx_web2
image: nginx:1.13.1
restart: always
ports:
- 80
expose:
- 80
links:
- phpfpm_web2
depends_on:
- phpfpm_web2
volumes:
- ./code:/usr/share/nginx/html
environment:
VIRTUAL_HOST: web2.local
VIRTUAL_PORT: 80
LETSENCRYPT_HOST: web2.local
LETSENCRYPT_EMAIL: foo#web2.local
volumes:
db_data_web2:
networks:
default:
external:
name: nginx-proxy
in php-fpm.d/www.conf, I tried the following ways, but none of them worked.
listen = 127.0.0.1:9000
listen = php_container_ipaddress:9000
listen = 9000
Many thanks in advance for your help!

run symfony2 app with docker [File not found.]

I'm trying to run my symfony app with docker. I have downloaded this bundle eko/docker-symfony
I have copy my symfony project inside of my docker directory.
If I refresh with localhost, I have this error: File not found.
If I put port 81 after localhost the page of Kibana will shop up.
When I run docker ps, I have noticed that the IP of all my containers is 0.0.0.0
docker-compose.yml
version: '2'
services:
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: symfony
MYSQL_USER: symfony
MYSQL_PASSWORD: symfony
php:
build: ./php-fpm
expose:
- "9000"
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
links:
- db
nginx:
build: ./nginx
ports:
- "80:80"
links:
- php
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- "81:80"
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- php
- nginx
symfony.conf
server {
server_name symfony.dev;
root /var/www/symfony/web;
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/symfony_error.log;
access_log /var/log/nginx/symfony_access.log;
}
I think the problem is port 80. This probably looks on your host for a file.
You can either try to run nginx under a different port f.e. 8001
nginx:
build: ./nginx
ports:
- "8001:80"
and call
http://localhost:8001
or use a hosts name. To quote the readme of eko/docker-symfony
...and do not forget to add symfony.dev in your /etc/hosts file.
like
127.0.0.1 symfony.dev
Then you can call
http://symfony.dev

Docker Compose: Nginx and PHP-FPM not working

I'm testing docker compose with Nginx and php-fpm, but this fail.
My docker-compose.yml:
version: '2'
services:
nginx:
container_name: nginx
build:
context: ./dockerfiles/nginx/
dockerfile: Dockerfile
volumes:
- ./project/:/usr/share/nginx/html/
ports:
- "8000:80"
links:
- php
php:
container_name: php-fpm
image: php:7-fpm
volumes:
- ./project/:/var/www/html/
ports:
- "9000:9000"
This is my dockerfile Nginx:
FROM nginx:latest
COPY config/default.conf /etc/nginx/conf.d/
And dafault.conf file:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
when I try localhost: 8000 returns the following message:
"File not found."
but, the index.php is in the project/ path.
that I am wrong?
I think you need to use volumes_from in your nginx container in the compose file, now you have in nginx:
volumes:
- ./project/:/usr/share/nginx/html/
And in php
volumes:
- ./project/:/var/www/html/
They should be the same.

Categories